Page 1 of 1

Understanding Bit-wise Operators in Java Rate Topic: -----

#1 Sheph  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 432
  • View blog
  • Posts: 1,019
  • Joined: 12-October 11

Posted 12 December 2011 - 02:30 AM

*
POPULAR

To understand how to use the bit-wise operators, it is necessary to understand what a bit is. A bit is nothing more than digit in the base 2 representation of a number. (Binary) Yeah, that's the cliche 00101001. The digits here are 0 or 1. So a bit can be a 0 or a 1. On or off. Open or closed, etc. In base 10 (Decimal), we have 10 digits starting at 0 and ending with 9. In base 2 (Binary), we have 2 digits starting at 0 and ending with 1. Simple right? Good let's move on...

Ever wonder why we say "base 2" or "base 10"? That's because the 10 or 2 is the base for conversion. Let's take the decimal number 101 (dalmations). That number's value is:
1          0          1
1*10^2  +  0*10^1  +  1*10^0  <--- notice the base is 10
1*100   +  0*10    +  1*1
100     +  0       +  1
101
Now we can see why we call it base 10. As we go right to left, the base remains the same, but the exponent is increased by 1. The first digit is multiplied by 10^0 (or 1). The second digit by 10^1 (or 10) and so on... So a base 2 number is nothing more than:
1         0         1
1*2^2  +  0*2^1  +  1*2^0
1*4    +  0*2    +  1*1
4      +  0      +  1
5
Aha! so the number 101 in binary code is what we know as 5 in decimal code.

Next step! The Byte! Alright computers have to store stuff in memory and they do so using 0's and 1's. But it would be impractical to store information in bits. How many things take just 1 bit to represent? Not very many things have just 2 states. So we have to use a Byte which is 8 bits long. So it can be represented by 8 digits of 0's or 1's. I know you've heard of a byte. How many Kilobytes does a file take up on your hard drive? 1 KB = 1000 bytes = 8000 bits.

Let's take the capital letter 'A' and represent it as a byte: 01000001. Now let's do stuff to it. ( Check out the ASCII table to get the values for different characters. 'A' is 65, I just converted it to binary. )

The First Operator - | inclusive OR operator
The | operator takes every bit of the left hand side and compares it with the right hand side. If at least one of them is a 1, the resultant bit is a 1. Only when they are both 0, does it stay 0. Let's 'A' | 'B'. Follow each bit in the 2 operands if at least one of them is a 1, mark a 1 in the answer's place.
'A'     = 01000001
'B'     = 01000010
__________________
'A'|'B' = 01000011 which is coincidentally 'C'!
You learned 'A'|'B'='C', but that is purely coincidence and 'B'|'C' doesn't equal D it equals 'C' again. :) Try it yourself and 'C'... I mean see. Now let's write some java code that demonstrates and proves me right since it is a Java tutorial
public static void main(String[] args) {
    int a = 'A';
    int b = 'B';
    int answer = a|b;
    System.out.print( (char)answer ); // Should print C
}


The & operator
This one is very similar to the | operator. The rules for this one is that the resultant bit is 1 only if BOTH bits are 1 in the operands. If at least one is a 0, the result is a 0. Let's take our example again. 'A' & 'B'
01000001
01000010
________
01000000
The only bit that was on in both of them was the second bit from the left, so it is the only bit that is still on after the operation.
public static void main(String[] args) {
    int a = 'A';
    int b = 'B';
    int answer = a&b;
    System.out.print( (char)answer ); // Should print @, which is 64 as an integer in decimal.
}


The ^ operator - Exclusive OR
This one is similar to the | operator, but it excludes the case where both are 1. This one only sets the resultant bit to 1 if both bits are different. If they are the same it is a 0. Our usual math:
01000001
01000010
________
00000011
And the code to support me:
public static void main(String[] args) {
    int a = 'A';
    int b = 'B';
    int answer = a^b;
    System.out.print( answer ); // Don't cast to char the value is not a normal symbol
}
And you'll see a 3 which is decimal for the result we got in binary. Great!

The ~ operator - Bitwise Complement
Let's turn that frown upside-down. Let's turn all you 0's to 1's and all you 1's to 0's. That's what this one does. Wanna see? Me too:
public static void main(String[] args) {
    int a = 'A'; // positive 65
    int answer = ~a;
    System.out.print( answer ); // I got -66 :)/>
}
This one is weird because it only returns an integer. You can cast it to other types. An integer is just 4 bytes in a row, so 32 consecutive bits. But you may have noticed something, it returns 1 less than the negative of the number. There is something called the two's complement, which takes the one's complement (this operation) and adds 1. It gives you the negative of a number. It is how many computers subtract. They simply add the two's complement (Or the negative version of the number).

The << and >> operators - Signed Left Shift and Right Shift
This one can be used to shift a pattern of bits to the left. For example if we had 00000101 our 101 dalmations, but we wanted the byte to start with 101, we could shift them 5 places to the left:
   00000101
<< 5
___________
   10100000
similarly, a right shift does exactly the opposite:
   10100000
>> 5
___________
   00000101
Let's prove it in Java.
public static void main(String[] args) {
    int a = 'A';
    a = a << 5;
    System.out.println( a );
    a = a >> 5;
    System.out.println( a ); // Should be 65, or 'A' if you cast to char
}


Well that about covers it. The one thing you should know is that in Java, all the types are signed, meaning the leftmost bit is usually restricted so it can keep track of the sign positive or negative. There are no unsigned types so to use all 8 bits in an unsigned manner you may need to use the next data type up. Meaning for a byte, you need the 2 byte data type (short). Happy bit-wise coding!

Is This A Good Question/Topic? 6
  • +

Page 1 of 1