C++ shift operator <<
does not cycle. For example if you do:
// C++
int a = 1;
cout << (a<<38);
You get 0. But, in Java you actually cycle and get a valid value of 64.
I need to translate some C++ code to Java, so what do I use as the equivalent for <<
?
The signed left shift operator " << " shifts a bit pattern to the left, and the signed right shift operator " >> " shifts a bit pattern to the right. The bit pattern is given by the left-hand operand, and the number of positions to shift by the right-hand operand.
The left shift operator shifts all bits towards the left by a certain number of specified bits. It is denoted by << . As we can see from the image above, we have a 4-digit number. When we perform a 1 bit left shift operation on it, each individual bit is shifted to the left by 1 bit.
The << operator shifts the left-hand value left by the (right-hand value) bits. Your example does nothing! 1 shifted 0 bits to the left is still 1. However, 1 << 1 is 2, 1 << 2 is 4, etc.
Operators in Java are used to performing operations on variables and values. Examples of operators: +, -, *, /, >>, <<. Assignment Operator. In this article, we will mainly focus on the Shift Operators in Java. By shifting the bits of its first operand right or left, a shift operator performs bit manipulation on data.
The Right Shift Operator shifts the bits of the number towards right a specified n number of positions. Right shift operator represented by the symbol >>, read as double greater than. When you write x>>n, the meaning is to shift the bits x towards the right n specified positions.
Signed Right Shift Operator in Java The Right Shift Operator moves the bits of a number in a given number of places to the right. The >> sign represents the right shift operator, which is understood as double greater than. When you type x>>n, you tell the computer to move the bits x to the right n places.
Most of the languages provide left shift operator using which we can left shift a number by certain positions and Java is one of them. The syntax of the left-shift operator in Java is given below, Return type: An integer after shifting x by n positions toward left
The Java language spec states:
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. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.
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. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x3f (0b111111). The shift distance actually used is therefore always in the range 0 to 63, inclusive.
So, in your example case, (int)(((long)a)<<38)
should work.
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. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.
Please refer to Java Language Specification: http://docs.oracle.com/javase/specs/jls/se7/jls7-diffs.pdf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With