How Does The Bitwise & (AND) Work In Java?
Solution 1
An integer is represented as a sequence of bits in memory. For interaction with humans, the computer has to display it as decimal digits, but all the calculations are carried out as binary. 123
in decimal is stored as 1111011
in memory.
The &
operator is a bitwise "And". The result is the bits that are turned on in both numbers. 1001 & 1100 = 1000
, since only the first bit is turned on in both.
The |
operator is a bitwise "Or". The result is the bits that are turned on in either of the numbers. 1001 | 1100 = 1101
, since only the second bit from the right is zero in both.
There are also the ^
and ~
operators, that are bitwise "Xor" and bitwise "Not", respectively. Finally there are the <<
, >>
and >>>
shift operators.
Under the hood, 123
is stored as either 01111011 00000000 00000000 00000000
or 00000000 00000000 00000000 01111011
depending on the system. Using the bitwise operators, which representation is used does not matter, since both representations are treated as the logical number 00000000000000000000000001111011
. Stripping away leading zeros leaves 1111011
.
Solution 2
It's a binary AND operator. It performs an AND operation that is a part of Boolean Logic which is commonly used on binary numbers in computing.
For example:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
You can also perform this on multiple-bit numbers:
01 & 00 = 00
11 & 00 = 00
11 & 01 = 01
1111 & 0101 = 0101
11111111 & 01101101 = 01101101
...
Solution 3
If you expand the two variables according to their hex code, these are:
bitmask : 0000 0000 0000 1111
val: 0010 0010 0010 0010
Now, a simple bitwise AND operation results in the number 0000 0000 0000 0010
, which in decimal units is 2. I'm assuming you know about the fundamental Boolean operations and number systems, though.
Solution 4
Its a logical operation on the input values. To understand convert the values into the binary form and where bot bits in position n have a 1 the result has a 1. At the end convert back.
For example with those example values:
0x2222 = 10001000100010
0x000F = 00000000001111
result = 00000000000010 => 0x0002 or just 2
Anzwur
Updated on September 25, 2022Comments
-
Anzwur over 1 year
I was reading through some code examples and came across a
&
on Oracle's website on their Bitwise and Bit Shift Operators page. In my opinion it didn't do too well of a job explaining the bitwise&
. I understand that it does a operation directly to the bit, but I am just not sure what kind of operation, and I am wondering what that operation is. Here is a sample program I got off of Oracle's website: http://docs.oracle.com/javase/tutorial/displayCode.html?code=http://docs.oracle.com/javase/tutorial/java/nutsandbolts/examples/BitDemo.java -
fge almost 11 years"123 in decimal is stored as 1111011 in memory" <-- not quite true... The machine may be little endian
-
fge almost 11 yearsOh no, not quite so... Not in Java. The JVM masks the difference for you. But in memory, the representation depends on the machine's endianness. It is very far from being an "implementation detail"!
-
user207421 almost 11 yearsIt still isn't relevant to the question.
-
fge almost 11 years@EJP disagree.
123
is NOT01111011
in memory in the most commonly used architecture in the market today. Giving a wrong idea about that is misleading at the very least, source of catastrophes in the worst case. -
starblue almost 11 years@Markus I would have upvoted you without that endianness crap. Note that the numbers don't even need to be in memory, when the operation is done on registers (most of the time).
-
R.M.VIVEK Arni over 9 yearsthe bitwise oprators based on zeros and ones ,AND,OR used to truth table based on output
-
Ian L about 7 yearsI had this problem in a multiple choice once and I only had 2 minutes to solve it.
out.println(1234<<6&76543);
. Is there any shorthand way of doing&
just with base 10 like how<<
multiplies by2
. -
sedavidw about 7 years@IanLimarta No. You will have to convert the numbers into binary, by splitting them into sums of powers of two. 1234 (base 10) = 1024 + 128 + 64 + 16 + 2 = 1*1024 + 0*512 + 0*256 + 1*128 + 1*64 + 0*32 + 1*16 + 0*8 + 0*4 + 1*2 + 0*1 = 10011010010 (base 2).