The Russian Peasant Algorithm
In maths class you’ve seen how arithmetic operators like addition, subtraction, multiplication, and division are integral to performing computations. They are ever present in the field of computer science (being a subset of maths, I’d argue maths is the representation of the principles of the universe) as well. Arithmetics in programming In programming languages, the same operators are used to perform arithmetic operations. func multiply(a, b int) int { return a * b } func add(a, b int) int { return a + b } func subtract(a, b int) int { return a - b } func divide(a, b int) int { return a / b } func modulo(a, b int) int { return a % b } func main() { fmt.Println(divide(6,3)) // 2 fmt.Println(subtract(4,1)) // 3 fmt.Println(add(2,3)) // 5 fmt.Println(multiply(2,3)) // 6 fmt.Println(modulo(14,2)) // 7 } Bitwise for computation The topic of focus here is multiplication. And how we can multiply two numbers without using the standard multiplication operator (*). Why would you want to do this? You may ask. Well, in the past when hardware wasn’t as powerful as it is today and resources were limited, systems programmers had to come up with more efficient ways of performing computations. One such way is utilizing bitwise operators to perform computations. An example of this was if you wanted to half a number, you would shift the number by one bit to the right (right shift >>). ...