4

I want to do left shifting but filling with zero, just like

int number = 20 >>> 10 = ( 0000 0000 0000 0000 0000 0000 0001 0100 ) 
int number = 20 >>> 32‎ =  ( 0000 0000 0000 0000 0000 0000 0000 0000 )

I want to do the same with left shift, since there is no operator <<< for leftshift

int number = 20 << 32 = 0000 0000 0000 0000 0000 0000 0001 0100 ;

I want to fill it with zeros just like >>> operator. So how I can do this?

icza
  • 389,944
  • 63
  • 907
  • 827
computerSPro
  • 45
  • 1
  • 6
  • 2
    This isn't clear; neither `>>> 32` nor `<< 32` have any effect, so what exactly are you asking? – Oliver Charlesworth Aug 30 '14 at 11:07
  • You've said that `20 >>> 32` gives you all-bits-off (`0000 0000 0000 0000 0000 0000 0000 0000`). I don't get all-bits-off, I get `0001 0100`. – T.J. Crowder Aug 30 '14 at 11:19
  • Read the [spec](http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.19): *If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance.* – Hot Licks May 20 '15 at 18:38

2 Answers2

17

The << left shift operator will bring in zeros just as you want to.

The reason why there are 2 right shift operators (>> and >>>) is because in 2's complement form negative numbers have a bit value of 1 in the left-most bit position. The right shift operator >> will add the sign bit (1 in case of negative numbers, and 0 in case of positive numbers or zero) on the left side while the other (>>>) will always add zeros.

Both right shift operators have their use.

The language specification states that if bit shifting operators are applied on int since it is 32-bit long, only the 5 lowest bit is used to determine how many times to shift the number.

So if you shift by 32 which is 100000 in binary, it is equivalent to shift by 0 which means not to shift! And if you want to shift a 64-bit long, the 6 lowest bit is used only to tell how many times to shift.

icza
  • 389,944
  • 63
  • 907
  • 827
  • Okay, i got it. i want to do like so in 32 bits : ( 20 << 32 ) but i got result = 20 and not 0, if the operator << fills with zero's then, i have to get 0; – computerSPro Aug 30 '14 at 11:13
  • When shifting an `int` number, only the 5 lowest bits are used to determine how many times to shift, and the 5 lowest bits of `32` are `00000` so no shift will happen. – icza Aug 30 '14 at 11:30
  • what heppens to sign bit on `left shift` ? Will it be un effected and only other bits are left shifted ? – Raghu Oct 27 '19 at 05:39
  • @Raghu The left shift is carried out as "normal", and the sign bit will be the bit that is shifted to that position. So it's possible that shifting a positive number becomes negative, and vice versa. E.g. `2147483647 << 1` will be `-2`, and `-2147483647 << 1` will be `2`. – icza Oct 29 '19 at 09:37
0

You need to use long type and only make use of lowest 32 bits. Use and operator to accomplish that.

    long intMask = 0x00000000FFFFFFFFL;
    long x = 0x00000000FFFFFFFFL;
    x = x<<32;
    int xi = (int)(x & intMask);
    System.out.println("xi=" + xi); //Result will be 0

See question : Declaring an unsigned int in Java

Community
  • 1
  • 1
Nejat76
  • 36
  • 2