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

- 603
- 1
- 6
- 10
-
27@jtahlborn This is what I hate about some of the users in SO. Why can someone not ask in stackoverflow first? Maybe they have some prior knowledge, but think stackoverflow is the best place to get their answers. – Mukus Mar 10 '14 at 22:41
-
@TejaswiRana - if you can't find answers to simple problems on your own on the web, then asking those questions and getting the answer through SO is going to cause more harm than good. you need to learn how to find answers to the easy stuff first. sometimes, to truly help someone, you have to tell them the hard truth, not the easy answers. – jtahlborn Mar 15 '14 at 01:39
-
17@jtahlborn If someone asks me which platform to go to in a train station, I will give them the easy answer. I don't know if that's just me but I expect the same from others too. I know that person could have searched before coming to the station, but the fact that I know they have more to do in life than listen to someone lecture, does not make any sense for me to give them any answer but the easy one. – Mukus Mar 15 '14 at 05:45
-
@TejaswiRana - i understand your perspective, and you are welcome to go through your life handing out fish (over and over again to the same people). i prefer to try to teach people to catch their own fish because i believe that that will serve them better in the long run. – jtahlborn Mar 15 '14 at 16:09
-
14@tjahlborn, him asking this question has helped me find an answer to what I was looking for, so I thank him and any others like him that don't do as you suggest. – null Apr 05 '15 at 08:47
8 Answers
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
.

- 8,370
- 15
- 50
- 83

- 86,735
- 21
- 136
- 138
-
1"123 in decimal is stored as 1111011 in memory" <-- not quite true... The machine may be little endian – fge Jun 23 '13 at 01:03
-
Oh 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"! – fge Jun 23 '13 at 01:22
-
7
-
@EJP disagree. `123` is **NOT** `01111011` 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. – fge Jun 23 '13 at 01:31
-
2@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). – starblue Jun 23 '13 at 06:59
-
I 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 by `2`. – Ian L Apr 05 '17 at 02:38
-
@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). – Markus Jarderot Apr 05 '17 at 05:52
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
...

- 37,233
- 13
- 109
- 109
If you look at two numbers represented in binary, a bitwise &
creates a third number that has a 1 in each place that both numbers have a 1. (Everywhere else there are zeros).
Example:
0b10011011 &
0b10100010 =
0b10000010
Note that ones only appear in a place when both arguments have a one in that place.
Bitwise ands are useful when each bit of a number stores a specific piece of information.
You can also use them to delete/extract certain sections of numbers by using masks.
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.

- 5,159
- 8
- 51
- 96
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

- 514
- 2
- 7
Knowing how Bitwise AND works is not enough. Important part of learning is how we can apply what we have learned. Here is a use case for applying Bitwise AND. Example:
Adding any even number in binary with 1's binary will result in zeros. Because all the even number has it's last bit(reading left to right) 0 and the only bit 1 has is 1 at the end.
If you were to ask write a function which takes an argument as a number and returns true for even number without using addition, multiplication, division, subtraction, modulo and you cannot convert number to string.
This function is a perfect use case for using Bitwise AND. As I have explained earlier. You ask show me the code? Here is the java code.
/**
* <p> Helper function </p>
* @param number
* @return 0 for even otherwise 1
*/
private int isEven(int number){
return (number & 1);
}

- 839
- 8
- 15
is doing the logical and digit by digit so for example 4 & 1 became
10 & 01 = 1x0,0x1 = 00 = 0
n & 1 is used for checking even numbers since if a number is even the oeration it will aways be 0

- 1,121
- 12
- 20
import.java.io.*;
import.java.util.*;
public class Test {
public static void main(String[] args) {
int rmv,rmv1;
//this R.M.VIVEK complete bitwise program for java
Scanner vivek=new Scanner();
System.out.println("ENTER THE X value");
rmv = vivek.nextInt();
System.out.println("ENTER THE y value");
rmv1 = vivek.nextInt();
System.out.println("AND table based\t(&)rmv=%d,vivek=%d=%d\n",rmv,rmv1,rmv&rmv1);//11=1,10=0
System.out.println("OR table based\t(&)rmv=%d,vivek=%d=%d\n",rmv,rmv1,rmv|rmv1);//10=1,00=0
System.out.println("xOR table based\t(&)rmv=%d,vivek=%d=%d\n",rmv,rmv1,rmv^rmv1);
System.out.println("LEFT SWITH based to %d>>4=%d\n",rmv<<4);
System.out.println("RIGTH SWITH based to %d>>2=%d\n",rmv>>2);
for(int v=1;v<=10;v++)
System.out.println("LIFT SWITH based to (-NAGATIVE VALUE) -1<<%d=%p\n",i,-1<<1+i);
}
}

- 4,102
- 5
- 23
- 37

- 1
- 3
-
the bitwise oprators based on zeros and ones ,AND,OR used to truth table based on output – R.M.VIVEK Arni Oct 14 '14 at 08:09