Java "Bit Shifting" Tutorial? [closed]
Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.
Closed 8 years ago.
Improve this questionI 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));
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.)
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
There is an infinite number of possible combinations. However they will be made up of one or more combinations of
>> shift right with sign extension.
>>> shift right with out sign extension.
<< shift left.
To get an understanding I suggest you write the binary numbers on paper and work out what happens. Trying to read it in a tutorial won't guarantee understanding. esp if they haven't helped so far.
There is simple but clear tutorial that I find useful here
It's not exactly a tutorial, but I have a personal library of bit-shifting functions in Java which you are very welcome to study!
Also if you do a google search for "bitwise tricks" you will find a lot of material. Many of these are in C/C++ but are generally trivially to convert to Java as most of the syntax is the same.
Here are the details of how bit shifting works. There is some non-intuitive behavior that is not covered by the official tutorial. For instance, the right operand has a limited range (0-31 for int, 0-63 for long), and will not produce a warning if you exceed that range -- it will just truncate the bits (i.e. %32 or %64), which may give behavior other than you expect.
This site seems to give a pretty good tutorial on what you can do with bit manipulation (so not specific to java but since it is pretty easy to translate)
http://www.bogotobogo.com/cplusplus/quiz_bit_manipulation.html
The tutorial above provides
- Bitwise Operations
- Setting and Clearing a Bit
- Displaying an Integer with Bits
- Converting Decimal to Hex
- The Number of Bits Set in an Integer (Number of Ones)
- The Bit Set Position of an Integer
- In-Place Integer Swap with Bit Manipulation
- The Number of Bits Required to Convert an Integer A to Integer B
- Swap Odd and Even Bits in an Integer
- What (n & (n-1) == 0) is checking?
- Two's Complement
- Fliping n-th bit of an integer
- Floating Point Number Bit Pattern
- Bit pattern palindrome of an integer
Here's a file that has a bunch of java implementations
http://geekviewpoint.com/
These are two good tutorials i found while learning about bit shifting, they arent in java but most languages use the same operators and the theory is the same.
- Bit twiddling
- PHP Bitwise Tutorial by Jim Plush
精彩评论