How does bitwitting work in Java?

I have this statement:

Suppose the value of the byte x is 00101011. What is the result of x>>2 ?

How can I program it, and can someone explain to me what it is doing?

+43
java bitwise-operators
Jul 22 '10 at 19:55
source share
10 answers

Firstly, you cannot shift byte in java, you can only shift int or long . Thus, byte will advance first, for example.

00101011 β†’ 00000000000000000000000000101011

or

11010100 β†’ 11111111111111111111111111010100

Now x >> N means (if you consider it as a string of binary digits):

  • The rightmost N bits are discarded
  • The leftmost bit is replicated as many times as necessary to fit the result in its original size (32 or 64 bits), for example.

00000000000000000000000000101011 >> 2 β†’ 00000000000000000000000000001010

11111111111111111111111111010100 >> 2 β†’ 11111111111111111111111111110101

+80
Jul 22 2018-10-22T00:
source share

Shift operators

Binary 32 bits for 00101011 :

00000000 00000000 00000000 00101011 , and the result:

  00000000 00000000 00000000 00101011 >> 2(times) \\ \\ 00000000 00000000 00000000 00001010 

Shifts bits 43 to the left by a distance of 2; filled with the highest (signed) bit on the left side.

The result is 00001010 with a decimal value of 10.

 00001010 8+2 = 10 
+44
Dec 04 '13 at 7:57
source share

When you change the right 2 bits, you drop the 2 least significant bits. So:

 x = 00101011 x >> 2 // now (notice the 2 new 0 on the left of the byte) x = 00001010 

This is essentially the same as dividing int by 2, 2 times.

In java

 byte b = (byte) 16; b = b >> 2; // prints 4 System.out.println(b); 
+14
Jul 22 '10 at 19:57
source share

These examples cover three types of shifts that apply to both positive and negative numbers:

 // Signed left shift on 626348975 00100101010101010101001110101111 is 626348975 01001010101010101010011101011110 is 1252697950 after << 1 10010101010101010100111010111100 is -1789571396 after << 2 00101010101010101001110101111000 is 715824504 after << 3 // Signed left shift on -552270512 11011111000101010000010101010000 is -552270512 10111110001010100000101010100000 is -1104541024 after << 1 01111100010101000001010101000000 is 2085885248 after << 2 11111000101010000010101010000000 is -123196800 after << 3 // Signed right shift on 626348975 00100101010101010101001110101111 is 626348975 00010010101010101010100111010111 is 313174487 after >> 1 00001001010101010101010011101011 is 156587243 after >> 2 00000100101010101010101001110101 is 78293621 after >> 3 // Signed right shift on -552270512 11011111000101010000010101010000 is -552270512 11101111100010101000001010101000 is -276135256 after >> 1 11110111110001010100000101010100 is -138067628 after >> 2 11111011111000101010000010101010 is -69033814 after >> 3 // Unsigned right shift on 626348975 00100101010101010101001110101111 is 626348975 00010010101010101010100111010111 is 313174487 after >>> 1 00001001010101010101010011101011 is 156587243 after >>> 2 00000100101010101010101001110101 is 78293621 after >>> 3 // Unsigned right shift on -552270512 11011111000101010000010101010000 is -552270512 01101111100010101000001010101000 is 1871348392 after >>> 1 00110111110001010100000101010100 is 935674196 after >>> 2 00011011111000101010000010101010 is 467837098 after >>> 3 
+6
Jan 24 '15 at 5:23
source share

>> is the operator of arithmetic right shift. All bits in the first operand are shifted by the number of places indicated by the second operand. The leftmost bits as a result are set to the same value as the leftmost bit in the original number. (This is so that negative numbers remain negative.)

Here is your specific case:

 00101011 001010 <-- Shifted twice to the right (rightmost bits dropped) 00001010 <-- Leftmost bits filled with 0s (to match leftmost bit in original number) 
+5
Jul 22 '10 at 19:59
source share
 public class Shift { public static void main(String[] args) { Byte b = Byte.parseByte("00101011",2); System.out.println(b); byte val = b.byteValue(); Byte shifted = new Byte((byte) (val >> 2)); System.out.println(shifted); // often overloked are the methods of Integer int i = Integer.parseInt("00101011",2); System.out.println( Integer.toBinaryString(i)); i >>= 2; System.out.println( Integer.toBinaryString(i)); } } 

Output:

 43 10 101011 1010 
+4
Jul 22 '10 at 20:27
source share

You cannot write binary literals such as 00101011 in Java, so you can write them in hexadecimal format:

 byte x = 0x2b; 

To calculate the result x >> 2 , you can simply write this exactly and print the result.

 System.out.println(x >> 2); 
+2
Jul 22 2018-10-22T00:
source share
 byte x = 51; //00101011 byte y = (byte) (x >> 2); //00001010 aka Base(10) 10 
+2
Jul 22 2018-10-22T00:
source share

You can use for example. this API if you want to see a bitString representation of your numbers. Unusual math

Example (in jruby)

 bitString = org.uncommons.maths.binary.BitString.new(java.math.BigInteger.new("12").toString(2)) bitString.setBit(1, true) bitString.toNumber => 14 

edit : api link changed and add a small example

+2
Jul 22 2018-10-22T00:
source share

00101011 = 43 in decimal form

 class test { public static void main(String[] args){ int a= 43; String b= Integer.toBinaryString(a >> 2); System.out.println(b); } } 

Output:

101011 becomes 1010

0
Sep 06 '16 at 19:27
source share



All Articles