-4
class shift_right {
    public static void main(String args[]) {
        char hex[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
        byte b = (byte) 0xf1;

        System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);
    }
}

I cant understand what's going on here

System.out.println("b = 0x" + hex[(b >> 4) & 0x0f] + hex[b & 0x0f]);

And why are taking 0xf1 and 0x0f.

  • 2
    Are you aware of bit-wise operations? If no then that would be a great starting point. – Yoshikage Kira May 25 '21 at 03:22
  • I know bit-wise operations but still I can't get this, why do we need to AND the number after right shifting with 0x0f cause the result is same after doing this operation that is 00001111. Please help me understand this. – Sourin Sarkar May 25 '21 at 15:40

1 Answers1

1

Just like I said in my comment, it seems that you do not have a clear grasp of how the bit wise operations work and how they can be used. My suggestion would be to do some exercises on bitwise operation so you know how they can be used. Nevertheless, I will go over what this code is doing?

What is this code doing?

This code is converting the byte stored in variable b into hexadecimal form. The hex array just allows us to convert decimal to hex form. This is more of an intermediate step.

Binary to Decimal?

Lets say we have byte 1010 1010. To convert this into hexadecimal we can refer to binary to hex table

Binary to Hex

We know that 1010 1010 is A A which can be written as 0xAA.

How this code works?

From the conversion above, we know that we can convert 4 bits into hex directly. And the code is doing the same thing. However, the issue we face is how exactly will we take a binary number and convert it to hex 4 bits at a time.

This is where bitwise operations into play. They allow us to slice, clear, set, toggle etc... bit with relative ease. Lets say we have a hex 1000 1010

We want to convert

1010 -> A

1000 -> 8

Bit wise AND can allow us to clear bits at certain position. If you AND a bit with 1, it remains unchanged. However, if you AND it with 0. Those bit get cleared.

Step 1: Convert 1010

1000 1010
0000 1111 (0x0F)
---------
0000 1010 <- the MSB 4 bits are now cleared. This will allow us to convert the 1010 to hex easily

1010 is 10 in decimal and the tenth element in array is 'A'. So hex[10] gives A.

Step 2: Convert 1000

If we just simply do a bit wise AND with 0xF0. The value we get is 1000 0000 which is -127, not 8. We somehow need to write it to 0000 1000 which is 8. This is where the shift to right comes in handy.

1000 1010 >> 4
---------
1111 1000 <- result of shifting 4 times

Now, you are probably wondering what the hell just happened. Why isn't the result of shifting 4 times 0000 1000?

When the bits are shifted to the right, the signed bits is extended and takes place of the bits that were shifted. This is called Arithmetic Shift which happens on signed byte(s) as opposed of Logical Shift which is for unsigned byte(s). Arithmetic Shift is very similar to Logical Shift if the number is positive if the leading bit is 0. So, now to clear all the bits that resulted in sign extension, we need to AND the number with 0x0F

1111 1000
0000 1111 (0x0F)
---------
0000 1000 This is 8 in decimal and hex[8] gives us '8'

So in the end we get "0x" + "8" + "A" which is "0x8A"

Yoshikage Kira
  • 1,070
  • 1
  • 12
  • 20