Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double Greater Than Sign (>>) in Java?

Tags:

java

What does the >> sign mean in Java? I had never seen it used before but came across it today. I tried searching for it on Google, but didn't find anything useful.

like image 763
Petey B Avatar asked Jun 26 '09 20:06

Petey B


People also ask

What is double greater than symbol in Java?

The >> operator is the bitwise right shift operator.

Can you use >= in Java?

In Java, Greater Than or Equal To Relational Operator is used to check if first operand is greater than or equal to the second operand. In this tutorial, we will learn how to use the Greater Than or Equal To Operator in Java, with examples. The symbols used for Greater Than or Equal To operator is >= .

What is << operator Java?

Left shift operator shifts the bits of the number towards left a specified number of positions. The symbol for this operator is <<.

How do you write the greater than sign in Java?

The symbols used for Greater Than operator is > . Greater Than operator takes two operands: left operand and right operand as shown in the following. The operator returns a boolean value of true if x is greater than y , or false if not.


2 Answers

The >> operator is the bitwise right shift operator.

Simple example:

int i = 4; System.out.println(i >> 1); // prints 2 - since shift right is equal to divide by 2 System.out.println(i << 1); // prints 8 - since shift left is equal to multiply by 2 

Negative numbers behave the same:

int i = -4; System.out.println(i >> 1); // prints -2 System.out.println(i << 1); // prints -8 

Generally speaking - i << k is equivalent to i*(2^k), while i >> k is equivalent to i/(2^k).

In all cases (just as with any other arithmetic operator), you should always make sure you do not overflow your data type.

like image 114
Yuval Adam Avatar answered Oct 15 '22 02:10

Yuval Adam


This is the bit shift operator. Documentation

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 unsigned right shift operator ">>>" shifts a zero into the leftmost position, while the leftmost position after ">>" depends on sign extension.

like image 43
Clint Avatar answered Oct 15 '22 00:10

Clint