• September 29th, 2011, 08:17 AM
chronoz13
i currently had a glance with bit operators in java such as Summary of Operators (The Java™ Tutorials > Learning the Java Language > Language Basics) bitwise and bit shift operators. given the following code i understand how they work, and some articles telling the usage and difference of logical operators vs usage of bitwise operators(e.g && and & , || and |)

Code :

```  System.out.println(3 & 6); // results in an integer 2 System.out.println(3 | 6); // results in an integer 7 System.out.println(3 ^ 6); // results in an integer 5```
i understand that the single &(bitwise AND), |(bitwise exclusive OR) and ^(bitwise inclusive OR) between 2 integers performs a Binary/Boolean multiplication and addition(given the logic of boolean arithmetic).

Code :

`System.out.println(16 << 3); // resutls in an integer 128`
this one shifts the binary value of 16, 3 times to the left, getting a value 128, vice versa if shifted using >> right shift operator, 16's binary value will be shifted to the right 3 times.

The double sign logical operators performs a short circuit operation, but a single sign logical operators(bitwise) always evaluate all the operands(e.g false & true, will be both evaluated). i found out that, although Bitwise operators can be used in a Boolean EXPRESSION/CONDITION, it was not intended for it, my conclusion was, it was intended for a Boolean/Binary arithmetic. is my conclusion right?

My question is, what is Bitmasking? is it a very technical hardcore thing to understand? articles i've read, doesnt give me a very handy answer.. maybe asking here might enlighten me given the knowledge i managed to learn with these operators..
• September 29th, 2011, 08:30 AM
Norm
Do you have an example definition of "bitmasking" that you don't understand that you could post here?
I've used the term to define a variable with some selected bits set on to be used to test the contents of another variable.
The "bit mask" variable is a template that is used to see if the selected bits in the tested variable are set on or not. Simple example. I want to see if a number is odd. The mask would be: 00001. If that mask is ANDed with a numeric variable and the results are not zero, then that variable has the tested bit on and the number is odd.
• September 29th, 2011, 08:48 AM
chronoz13
this article masking : Java Glossary a java glossary defines some meaning and examples for what is masking is about using the bit operators. its a bit complicated in my standpoint right now, so thanks for giving a basic sample of the term bitmasking, so it can be used for an ODD / EVEN number checking program, other than a modular arithmetic.
• September 29th, 2011, 08:58 AM
chronoz13
i got the ODD/EVEN thanks norm :D
• September 29th, 2011, 09:02 AM
Norm
1 & 7 = 1 NOT 0
Not 0 means the number is odd

What are you expecting?
• September 29th, 2011, 09:03 AM
helloworld922
Other way around:

if the LSB (least significant bit, or bit furthest to the right) is set, a binary number is odd. Otherwise it's even.

Code Java:

`System.out.println((7 & 0x1) == 0); // true if 7 is even`

Also, it's much easier to use binary and hexadecimal numbers vs. decimal numbers when dealing with bit masking/shifting.

edit: haha, I let convention and early mornings slip my mind over reality. It doesn't matter which operand is the mask and which is the value. Though everything else above is true.
• September 29th, 2011, 09:05 AM
chronoz13
Quote:

1 & 7 = 1 NOT 0
Not 0 means the number is odd

What are you expecting?
yes, sorry i was not paying enough attention on my code,
• September 29th, 2011, 10:00 AM
Sean4u
Quote:

if the LSB (least significant bit, or bit furthest to the right) is set, a binary number is odd. Otherwise it's even
Is it 'use implementation detail week'? :D

Checking the LSB for negative numbers relies on ints being represented that way. An even number is a number for which there's no remainder after division by 2. There are good uses of bitmasking, but the odd/even test is not one of them:

c - How do I check if an integer is even or odd? - Stack Overflow
• September 29th, 2011, 10:22 AM
Norm