I have to do Boolean operation like,
B1=00 10 11 01 00
B2=00 10 11 01
B1 NOR B2 =11 11 11 11
How is it possible in Java ? Can anybody help ?
Sorry, I forgot to mention, is it possible in Java to perform this bit wise NOR operation from the left always without shifting (B2 has less bits) ?
If
b1andb2are your (int) variables, the expression would be:For more info, see Bitwise and Bit Shift Operators.
However, in your example the variables have different lengths. The result doesn’t correspond to a NOR either.
Update: if you have two sequences of boolean values, no equal size, and only need a portion of it (left to right), I’d suggest using a
BitSet:If you really want to use
int, then it’s better to represent it right to left (i.e. the first bit isb % 2, the second isb / 2 % 2, etc. Then you just have to do the NOR as explained in the beginning, and if necessary truncate the result.If they are represented left to right, I believe a shift will be necessary (or a multiplication/division, which is essentially the same thing, but more expensive…), unless they are padded with 0’s (ex.:
B1=0010 1101 0000 0000 0000 0000 0000 0000,B2=0010 1101 0000 0000 0000 0000 0000 0000). In this case you can do the operation normally.