Java "Bit Shifting" Tutorial?

I would be grateful for a good tutorial that explains for java beginners how in java all “bit offsets” work.

I always stumbled upon it, but never understood how it works. He should explain all the operations and concepts that are possible using the byteshifting / bitmanipulation function in java.

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

byte b = (byte)(l >> (8 - i << 3)); 
+45
java bit-manipulation bit-shift
Jun 06 2018-11-11T00:
source share
8 answers

Well, the official Java tutorial Bitwise and bit shift operators describe the actual operations available in Java and how to call them.

If you are interested in “what can I do with the permutation of bits”, then this is not specific to Java, and since this is a low-level technique, I don’t know a single list of “cool things you can” do as such. It would be helpful to familiarize yourself with the definitions and open your eyes to another code, where it is used, to see what they have done.

Please note that often bit-twiddling is a gain in efficiency due to clarity. For example, a << 1 usually matches a * 2 , but perhaps less clear. Repeated XORs can change two numbers without using a temporary variable, but it is usually considered the best form for writing code with a temporary variable more clearly (or even better, in the utility method). Therefore, it’s difficult to give excellent examples in this regard, because you are unlikely to achieve something new or a deep level of architecture; all about low level details. (And I would appreciate that the sheer number of uses of bit-twiddling "in the wild" are examples of premature optimization.)

+34
Jun 06 2018-11-11T00:
source share

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

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

"In some languages, applying shift operators to any data type smaller than int automatically changes the size of the operand, INT."

It is absolutely important to remember, for example, when working with bytes, otherwise you may get unexpected results (like me).

The specified byte with the following bit pattern:

 1001 0000 

When I tried to shift the bit by 4 and assign an int, for example:

 int value = byteValue >>> 4; 

I would expect:

 0000 1001 (or a value of 9) 

But I would get a HUGE number! This is because byteValue is passed to the bit offset operation in int BEFORE , which leads to something like this:

 1111 1111 1111 1111 1111 1111 1001 
+14
Nov 07
source share

There are an infinite number of possible combinations. However, they will consist of one or more combinations

 >> shift right with sign extension. >>> shift right with out sign extension. << shift left. 

To understand, I suggest you write binary numbers on paper and find out what happens. Trying to read it in a textbook does not guarantee understanding. esp if they haven't helped yet.

+13
Jun 06 2018-11-11T00:
source share

There is a simple but clear tutorial that I find useful here

+9
Jun 06 2018-11-11T00:
source share

This is not quite a tutorial, but I have a personal library of bit-shifting functions in Java, which you are very welcome to learn!

Also, if you search for bitwise tricks , you will find a lot of material. Many of them are in C / C ++, but it is usually trivial to convert to Java, since most of the syntax is the same.

+5
Jun 06 2018-11-11T00:
source share

The following is information on how bit shifting works . There is unintuitive behavior that does not apply to official management. For example, the right operand has a limited range (0-31 for int, 0-63 for long) and will not cause warnings if you exceed this range - it will simply truncate bits (i.e.% 32 or% 64), which may lead to behavior other than expected.

+5
Aug 21 '13 at 16:01
source share

This site seems to give a pretty good tutorial on what you can do with bit manipulation (therefore not the case with java, but since it's pretty easy to translate)

http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.html

In the tutorial below

  • Bitwise operations
  • Setting and clearing bits
  • Bits integer mapping
  • Convert decimal to hexadecimal
  • The number of bits specified in integers (number of units)
  • Bit set position of integers
  • Entering an integer swap with bit manipulation
  • The number of bits needed to convert an integer A to an integer B
  • Even and even bit exchange in integers
  • What does (n and (n-1) == 0) check?
  • Two additions
  • Reflection of the nth bit of an integer
  • Bit bit floating point numbers
  • The integer bit palette palindrome

Here's a file having a bunch of java implementations

http://geekviewpoint.com/

+3
Sep 28 '11 at 12:11
source share

These are two good tutorials that I found while studying bit offsets, they exist in java, but most languages ​​use the same operators, and the theory is the same.

+2
Feb 20 '12 at 19:08
source share



All Articles