Bitwise Operation

Home Physics Tools Mathematical Tools Bitwise Operation


Decimal to Hexadecimal and
Hexadecimal to Decimal Converter


Online Bitwise Calculator

In computer programming, a bitwise operation operates on one or two bit patterns or binary numerals at the level of their individual bits. On many computers, bitwise operations are slightly faster than addition and subtraction operations and significantly faster than multiplication and division operations.


The bitwise operators utilize something called Binary Math. If you already know binary math, skip ahead. Binary math is a BASE 2 numbering system, where as our normal everyday numbering system is a BASE 10 system. Think back to elementary school where we learned numbers this way...

Bitwise Operators

ExampleNameResult
A & BAndBits that are set in both A and B are set.
A | BOrBits that are set in either A or B are set.
A ^ BXor Bits that are set in A or B but not both are set.
~ ANot Bits that are set in A are not set, and vice versa.
A << BShift left Shift the bits of A B steps to the left (each step means "multiply by two")
A >> BShift right Shift the bits of A B steps to the right (each step means "divide by two")


Lets use two variables to get started with the "And" operator &.

The & or "And" operator takes two values and returns a decimal version of the binary values the left and right variable share. So using our tables above we can see that the only bit these two share is in the 8 position so A & B = 8.

A = 9
B =10
A & B

This would output the number 8. Why?? Well lets see using our table example

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
A 0 0 0 0 1 0 0 1 = 9
B 0 0 0 0 1 0 1 0 = 10

So you can see from the table the only bit they share together is the 8 bit. So 8 gets returned.. not too hard eh? Lets look at another example of the & operator.

A = 36;
B =103;
A & B

This would output the number 36. Why?? Well lets see using our table example again

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
A 0 0 1 0 0 1 0 0 = 36
B 0 1 1 0 0 1 1 1 = 103

So you can see the only bits these two share together are the bits 32 and 4 which when added together return 36. This operator is saying "I want to know what bits you both have set in the same column"


Lets move on to the | also known as the "OR" operator.

A = 9
B =10
A | B

This would output the number 11. Why?? Well lets see using our table example

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
A 0 0 0 0 1 0 0 1 = 9
B 0 0 0 0 1 0 1 0 = 10

If you notice we have 3 bits set, in the 8, 2, and 1 column.. add those up 8+2+1 and you get 11. It is just saying "I want to know what bits either one of you guys have set".


Moving on to the ^ operator also known as the "Xor" operator.
A = 9
B =10
A ^ B

This would output the number 3. Why?? Well lets see using our table example

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
A 0 0 0 0 1 0 0 1 = 9
B 0 0 0 0 1 0 1 0 = 10

The XOR operator wants to know "Tell me what bits you both have set but I dont want any bits you share" Notice we have 3 bits set but both A and B share the 8 bit, we dont want that one, we just want the 2 bit and the 1 bit that they each have set but don't share. Soooo 2+1 = 3


OK, here is one that gets tricky the ~ operator also known as the "NOT" operator.
A = 9
B =10
A & ~B

This would output the number 1. Why?? Well lets see using our table example

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
A 0 0 0 0 1 0 0 1 = 9
B 0 0 0 0 1 0 1 0 = 10

The NOT operator wants to know what is set in A but NOT set in B because we marked B with the ~operator in front of it. So looking at our table we can see the only bit set in A thats not in B is 1.

What happens if we do this...

A = 9
B =10
~A & B

We get the value 2, because we want the bits set in B but NOT set in A this time, so since they both share the 8 bit, 2 bit is the only one B has that A does not.


Bit Shifting

A = 16;
A << 2;

This would output the number 64. Why?? Well lets see using our table example

1 Byte ( 8 bits )
Place Value 128 64 32 16 8 4 2 1
A - BEFORE! 0 0 0 1 0 0 0 0 = 16
A - AFTER! 0 1 0 0 0 0 0 0 = 64

How do we get 64? well bit shifting tells to take the variable A which in our case is 16 and shift if 2 bits which is basically like saying take 16 and multiply it by 2 twice. So 16 X 2 = 32 x 2 = 64



Enter two decimal values and select the operator, then click Calculate button to see how binary conversion done and how to apply bitwise operator for binary result.

AND , OR , XOR
Decimal Val. #1
Decimal Val. #2