How can I multiply and divide using only bit shifting and adding?
问题 How can I multiply and divide using only bit shifting and adding? 回答1: To multiply in terms of adding and shifting you want to decompose one of the numbers by powers of two, like so: 21 * 5 = 10101_2 * 101_2 (Initial step) = 10101_2 * (1 * 2^2 + 0 * 2^1 + 1 * 2^0) = 10101_2 * 2^2 + 10101_2 * 2^0 = 10101_2 << 2 + 10101_2 << 0 (Decomposed) = 10101_2 * 4 + 10101_2 * 1 = 10101_2 * 5 = 21 * 5 (Same as initial expression) ( _2 means base 2) As you can see, multiplication can be decomposed into