I recently have started learning Java, and now I am covering the bit operator part. While studying, I was wondering when this bitwise operators are used, and I would like you to give me some examples if possible. Thank you!
-
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op3.html – Kick Buttowski Sep 14 '14 at 06:49
-
possible duplicate of [When to use Bitwise Operators during webdevelopment?](http://stackoverflow.com/questions/261062/when-to-use-bitwise-operators-during-webdevelopment) – Kick Buttowski Sep 14 '14 at 06:50
-
1Can you narrow it down? When you googled this, what links did you turn up, and what questions do you have about those links? – John Kugelman Sep 14 '14 at 06:53
-
For example, in one line, you can check if number is power of 2: `if(num & -num == num) { }`.. Without bitwise operators, it would take 5 lines of code. – Maroun Sep 14 '14 at 06:54
-
Same as any other kind of operator: when necessary. – harold Sep 15 '14 at 15:44
3 Answers
Good example - bitwise XOR to swap two numbers (again, very popular in interviews) - fast swapping values without any third variable:
int a = 2; // a = 0010
int b = 11; // b = 1011
a = a ^ b; // a = 0010 ^ 1011 = 1001
b = a ^ b; // b = 1001 ^ 1011 = 0010 (as a at the beginning)
a = a ^ b; // a = 1001 ^ 0010 = 1011 (as b at the beginning)
You can find an article about this in wiki

- 108
- 7
There's several places, though they aren't things that you will use often. You'll just end up using them when you need them.
A good example is checking if a number is even:
if (num & 1 == 0) {}
They are also useful in flags, such as having this:
private static final int ENABLE_FOO = 0x0001;
private static final int ENABLE_BAR = 0x0002;
static int mask = (ENABLE_FOO | ENABLE_BAR);
public static void example() {
if (mask & ENABLE_FOO) { //If flag set.
do_foo();
}
if (mask & ENABLE_BAR) { //If flag set.
do_bar();
}
}
public static void doFooOnce() {
if (mask & ENABLE_FOO) { //If flag set.
do_foo();
}
mask &= ~ENABLE_FOO; //Bitwise and mask by the bitwise opposite of ENABLE_FOO
}
There's other places, too. Just know that you won't use them too often, but when you do they will be useful.

- 4,984
- 9
- 37
- 62
Bitwise operators are used for bit manipulation, i.e. in cases when you want to go down to "gory details" of data structures that in the end of the day are sequences of bytes.
There are a lot of tutorials that explain various usages of bitwise operators, however I will give you only one that (IMHO) is the most useful (at least for me).
Sometimes you want to handle a lot of boolean flags. You can create Map<String, Boolean>
and (for example) pass instance of such map to some method (let's call it foo()
), i.e.:
Map<String, Boolean> options = new HashMap<>();
// fill the map
foo(options);
Obviously we can use enum and EnumMap instead of string keys.
Alternatively we can define a series of constants like:
public static final int ONE = 1; public static final int TWO = 2; public static final int THREE = 4; public static final int FOUR = 8;
etc. etc.
Now we can change foo()
to get int
parameter and call it as following:
foo(ONE | TWO);
foo(ONE | FOUR);
etc.
In some cases this notation is more readable; in most cases it saves memory and gives some performance benefits.
Please note that already mentioned EnumMap
is implemented using this technique, so you can just use it in most cases and enjoy both efficiency and OOD.

- 114,158
- 16
- 130
- 208