2

Recently I have been learning about bitwise operators and along the way there is this code that finds the binary digits of a decimal number using the AND(&) bitwise operator, the code is as follows:

 byte b = -34;

 for(int t = 128;t > 0; t = t/2)
{
 if((b & t) != 0)System.out.println("1 ");
 else System.out.println("0 ");
 System.out.println("b & t yields: " + (b & t));
   }

I have modified the code to show the value calculated by b&t during each iteration. I would like to understand the exact mechanism behind this code as to why it works to find the binary digits, please explain why is b compared to t each iteration and why is t divided by 2 each iteration?

In addition, I would like to know how is (b&t) calculated manually by listing the binary digits.I do have an understanding how & works but when I listed out the binary digits of 34 and 128 and compared them:

1 0 0 0 0 0 0 0(128)
0 0 1 0 0 0 1 0(34) //I am unsure if the negative sign should be included
---------------
0 0 0 0 0 0 0 0

the result I got was 0, however the program returns 128 which is perplexing.

Below I will also include the result of the execution of the program:

1 
b & t yields: 128
1 
b & t yields: 64
0 
b & t yields: 0
1 
b & t yields: 16
1 
b & t yields: 8
1 
b & t yields: 4
1 
b & t yields: 2
0 
b & t yields: 0

Much obliged for the help :)

2
  • 2
    Nowadays computers use two's complement, so -34 is represented by 256-34 as a unsigned byte. Commented Jun 10, 2017 at 15:59
  • 1
    Thanks very much,it makes sense that -34&128 return 128 now :) Commented Jun 10, 2017 at 16:08

2 Answers 2

2

Dividing t by 2 is a bit-shift to the right:

1 0 0 0 0 0 0 0 128 = t
0 1 0 0 0 0 0 0 64  = t / 2
0 0 1 0 0 0 0 0 32  = t / 2 / 2
...

t always has one bit set to 1, all others are 0.

Then you compare that to b using &. Each result bit is 1 if and only if the corresponding bit in both inputs is 1 as well. That means that we basically check if the bit in b is 1 at the location that the t-bit is 1. That is done for all bits from left to right.

Sign up to request clarification or add additional context in comments.

Comments

0

Since there were two questions: (1) OP is perplex why his hand-calculation does not match with program's code and (2) OP would like to know why one of the variables is divided by 2.

I merely combine the answers: (1) Negative numbers are represented as two's complement. Therefore negative 34 is

 0 0 1 0 0 0 1 0 <-- +34
 1 1 0 1 1 1 0 1 <-- one's complement of 34
 1 1 0 1 1 1 1 0 <-- two's complement of 34

Note: two's complement is one's complement + 1.

(2) Division by 2 is shifting to the right (if it is binary). That's why, the third time in the loop, it outputs zero (The only 1 in 128 is 'AND'ed with the third zero in -34).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.