Java Bitwise Or

Posted: January 11, 2010 in AP Computer Science A
Tags: , , , , ,

Java Bitwise Or

System.out.print(15|32) returns 47

which is tricky because you assume that addition is taking place. But the following line

System.out.print(11|33) returns 43

This is not addition, it is called the bitwise Or. To illustrate:

Step 1: Convert the integers to binary lining up the digits in place value.

11 = 001011
33 = 100001

Step 2: Or the digits in corresponding place values (right to left or left to right doesn’t matter in the case of Or)

0 0 1 0 1 1
OR 1 0 0 0 0 1
Result 1 0 1 0 1 1

When using an Or, if both digits are 0 the result is 0. All other cases result in 1.

Step 3: Convert to decimal if necessary.

101011 = 43

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s