Java “Bit Shifting” Tutorial? [closed]

2019-01-05 00:58发布

I would be thankfull 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));

8条回答
Bombasti
2楼-- · 2019-01-05 01:01

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.

查看更多
Rolldiameter
3楼-- · 2019-01-05 01:02

There is simple but clear tutorial that I find useful here

查看更多
趁早两清
4楼-- · 2019-01-05 01:04

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.

  1. Bit twiddling
  2. PHP Bitwise Tutorial by Jim Plush
查看更多
我只想做你的唯一
5楼-- · 2019-01-05 01:11

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
查看更多
6楼-- · 2019-01-05 01:13

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/

查看更多
霸刀☆藐视天下
7楼-- · 2019-01-05 01:19

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.

查看更多
登录 后发表回答