Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Java triple shift operator (>>>) in C#?

What is the equivalent (in C#) of Java's >>> operator?

(Just to clarify, I'm not referring to the >> and << operators.)

like image 506
Nikolaos Avatar asked Dec 10 '09 10:12

Nikolaos


People also ask

What is the use of >>> operator in Java?

The >>> operator is the unsigned right bit-shift operator in Java. It effectively divides the operand by 2 to the power of the right operand, or just 2 here.

What is the difference between >> and >>> in Java?

>> 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).

What is Java shift operator?

The shift operator is a java operator that is used to shift bit patterns right or left.

Can you Bitshift in Java?

The Java programming language also provides operators that perform bitwise and bit shift operations on integral types.


2 Answers

In C#, you can use unsigned integer types, and then the << and >> do what you expect. The MSDN documentation on shift operators gives you the details.

Since Java doesn't support unsigned integers (apart from char), this additional operator became necessary.

like image 50
Lucero Avatar answered Oct 01 '22 22:10

Lucero


Java doesn't have an unsigned left shift (<<<), but either way, you can just cast to uint and shfit from there.

E.g.

(int)((uint)foo >> 2); // temporarily cast to uint, shift, then cast back to int 
like image 23
Matt Avatar answered Oct 01 '22 22:10

Matt