Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of C++ shift operator << in Java?

Tags:

java

c++

porting

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 <<?

like image 583
user1796942 Avatar asked Jan 15 '13 18:01

user1796942


People also ask

What is the << operator in Java?

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.

How do you shift left in Java?

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.

What does << In C mean?

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.

What are shift operators in Java?

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.

What is right shift operator in C?

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.

What does >> mean in Java right shift?

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.

How to left shift a number by certain positions in Java?

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


2 Answers

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.

like image 186
Hot Licks Avatar answered Sep 28 '22 08:09

Hot Licks


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

like image 25
shuangwhywhy Avatar answered Sep 28 '22 10:09

shuangwhywhy