difference between and Â using -'java,syntax,operators'

0 votes

0 votes

`>>`

is arithmetic shift right, `>>>`

is logical shift right.

In an arithmetic shift, the sign bit is extended to preserve the signedness of the number.

For example: -2 represented in 8 bits would be `11111110`

(because the most significant bit has negative weight). Shifting it right one bit using arithmetic shift would give you `11111111`

, or -1. Logical right shift, however, does not care that the value could possibly represent a number; it simply moves everything to the right and fills in from the left with 0s. Shifting our -2 right one bit using logical shift would give `01111111`

.

0 votes

They are both right-shift, but `>>>`

is `unsigned`

From the documentation:

The unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

0 votes

`>>>`

is unsigned-shift; it'll insert 0. `>>`

is signed, and will extend the sign bit.

The shift operators include left shift

`<<`

, signed right shift`>>`

, and unsigned right shift`>>>`

.The value of

`n>>s`

is`n`

right-shifted`s`

bit positions withsign-extension.The value of

`n>>>s`

is`n`

right-shifted`s`

bit positions withzero-extension.

```
System.out.println(Integer.toBinaryString(-1));
// prints "11111111111111111111111111111111"
System.out.println(Integer.toBinaryString(-1 >> 16));
// prints "11111111111111111111111111111111"
System.out.println(Integer.toBinaryString(-1 >>> 16));
// prints "1111111111111111"
```

- Right Shift to Perform Divide by 2 On -1
- Is shifting bits faster than multiplying and dividing in Java? .NET?
- what is c/c++ equivalent way of doing â€˜>>>â€™ as in java (unsigned right shift)
- Negative logical shift
- Javaâ€™s >> versus >>> Operator?
- What is the difference between the Java operators >> and >>>?
- Difference between >>> and >> operators
- Whatâ€™s the reason high-level languages like C#/Java mask the bit shift count operand?
`1 >>> 32 == 1`

0 votes

Read more about Bitwise and Bit Shift Operators

```
>> Signed right shift
>>> Unsigned right shift
```

The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand. The unsigned right shift operator `>>>`

*shifts a zero into the leftmost position*,

while the leftmost position after `>>`

depends on sign extension.

In simple words `>>>`

always *shifts a zero into the leftmost position* whereas

`>>`

shifts based on sign of the number i.e. 1 for negative number and 0 for positive number.For example try with negative as well as positive numbers.

```
int c = -153;
System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
System.out.println(Integer.toBinaryString(c <<= 2));
System.out.println();
c = 153;
System.out.printf("%32s%n",Integer.toBinaryString(c >>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c >>>= 2));
System.out.printf("%32s%n",Integer.toBinaryString(c <<= 2));
```

output:

```
11111111111111111111111111011001
11111111111111111111111101100100
111111111111111111111111011001
11111111111111111111111101100100
100110
10011000
100110
10011000
```

0 votes

The right shift logical operator (`>>> N`

) shifts bits to the right by N positions, discarding the sign bit and padding the N left-most bits with 0's. For example:

```
-1 (in 32-bit): 11111111111111111111111111111111
```

after a `>>> 1`

operation becomes:

```
2147483647: 01111111111111111111111111111111
```

The right shift arithmetic operator (`>> N`

) also shifts bits to the right by N positions, but preserves the sign bit and pads the N left-most bits with 1's. For example:

```
-2 (in 32-bit): 11111111111111111111111111111110
```

after a `>> 1`

operation becomes:

```
-1: 11111111111111111111111111111111
```

...