Bitwise Multiply
just a quick ques开发者_JS百科tion , your code does add up bits to numbers which are higher than 2 which is binary's limit ,for instance suppose n1 = 1111 and n2 is the same , then your code will show the result of 1234321 , though its not a valid binary number , How should we make the result be in binary ? thanks
In Java you can do the following to get output in binary
int i1 = 1111;
int i2 = 1111;
int result = i1 * 12;
System.out.println("result in binary " + Integer.toBinaryString(result));
The algorithm is the same as with decimal numbers, like you learned in school. You carry the overflow. But really, that has nothing to do with multiplication. That's just addition. For instance, 1111 x 1111 = 1111 + 11110 + 111100 + 1111000.
You are msisunderstanding binary math. 1111 is not a binary, anyway not the 1111 which can be multipled by 1111 and results 1234321.
a binary 1111 would be 15 in Decimal. try using the windoes Calc to change between Dec. and Bin.
[1111 (Dec) = 10001010111 (Bin)
1111 (Bin) = 15 (Dec)
1234321 (Dec) = 100101101010110010001 (Bin)
1234321 (bin) is invlid][1]
Anyway you will need to go through the binary Math in order to understand how this work.
Try this link and see how multiplication works
精彩评论