Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java "Bit Shifting" Tutorial? [closed]

I would be thankful for a good tutorial, that explain for java newbies how in java all the "bit shifting" work.

I always stumble across it, but never understood how it works. It should explain all the operations and concepts that are possible with byteshifting/bitmanipulation in java.

This is just an example what I mean, (but I am looking for a tutorial that explains every possible operation):

byte b = (byte)(l >> (8 - i << 3)); 
like image 948
Markus Avatar asked Jun 06 '11 09:06

Markus


People also ask

Can you do bit shifting in Java?

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

What does shift () do in Java?

A shift operator performs bit manipulation on data by shifting the bits of its first operand right or left. The next table summarizes the shift operators available in the Java programming language. Each operator shifts the bits of the first operand over by the number of positions indicated by the second operand.

How do you shift binary in Java?

Java supports two types of right shift operators. The >> operator is a signed right shift operator and >>> is an unsigned right shift operator. The left operands value is moved right by the number of bits specified by the right operand.


2 Answers

Well, the official Java tutorial Bitwise and Bit Shift Operators covers the actual operations that are available in Java, and how to invoke them.

If you're wondering "what can I do with bit-shifting", then that's not Java specific, and since it's a low-level technique I'm not aware of any list of "cool things you can" do per se. It'd be worth becoming familiar with the definitions, and keeping your eyes open for other code where this is used, to see what they've done.

Note that often bit-twiddling is an efficiency gain at the expense of clarity. For example, a << 1 is usually the same as a * 2 but arguably less clear. Repeated XORs can swap two numbers without using a temporary variable, but it's generally considered better form to write the code more clearly with the temporary variable (or even better, in a utility method). So in this respect it's hard to give great examples, because you're not likely to achieve anything new or profound on an architecture level; it's all about the low-level details. (And I'd estimate that a vast number of uses of bit-twiddling "in the wild" are instances of premature optimisation.)

like image 159
Andrzej Doyle Avatar answered Sep 20 '22 19:09

Andrzej Doyle


When using the shift operator, be very careful not to repeat a common error!!

As the following SO post suggests, the author of the accepted answer mentions:

"In some languages, applying the shift operators to any datatype smaller than int automatically resizes the operand to be an int."

This is absolutely crucial to remember when operating on bytes for example, otherwise you may get unexpected results (as I did).

Given a byte with the following bit pattern:

1001 0000 

When I tried to bit shift by 4, and assigned to an int, such as:

int value = byteValue >>> 4; 

I would expect to have:

0000 1001   (or a value of 9) 

But I would get a HUGE number! That's because the byteValue is casted to int BEFORE the bit shift operation, thus resulting in something like this instead:

1111 1111 1111 1111 1111 1111 1001 
like image 22
Jeach Avatar answered Sep 21 '22 19:09

Jeach