Bitwise Operations

 
 




Input
Result

Here you can evaluate expressions formed using bitwise operators (from C++, Java and JavaScript).
The numbers used are stored and displayed using 2 bytes or 16 bits.
The result is always output in decimal, hexadecimal (0x...), and binary (0b...).
Therefore, the program can also be used simply as a number converter.
A hexadecimal digit represents a group of four binary digits (nibbles).
In the output, these groups of four adjacent binary digits are displayed alternately in red and black.

Allowed operators are (operator priority decreasing in this order):

˜ negate bit by bit
<<, >> shift left or right
& bitwise and
^bitwise exclusive or
|bitwise or

The following number literals are accepted as input:
Decimal numbers such as 474,
Binary numbers such as 0b111011010,
Hexadecimal numbers such as 0x1da.

In hexadecimal representation, there are 16 different digits or nibbles,
whose bit assignment is shown below:

dechexbin
00x0 0000
10x1 0001
20x2 0010
30x3 0011
40x4 0100
50x5 0101
60x6 0110
70x7 0111
80x8 1000
90x9 1001
100xa 1010
110xb 1011
120xc 1100
130xd 1101
140xe 1110
150xf 1111

Application Examples

Bits are frequently used to store simple information compactly.
Bit operations allow you to selectively set and clear specific bits in an integer memory variable.
Individual bits are accessed via their position in the memory variable.
The rightmost bit is at position 0.
The position is then incremented from right to left.

Setting bits
135 | 1<<4
In a memory location initially allocated 135, bit 4 is to be set.
To do this, the 1 (bit at position 0) is first shifted 4 positions to the left, and then
by using an OR operation with the "memory size" 135, it is forced that bit 4 is set as a result.
This then yields 151.

Erase bits
135 & ~(1<<2)
In a memory location initially assigned 135, bit 2 is to be cleared.
First, a 1 is shifted to bit position 2, then the bit's complement is calculated.
This results in a bit assignment that is 1 everywhere except for position 2.
The AND operation with the "memory contents" of 135 forces bit 2 to be cleared.
This then yields 131.

Using Variables
a=135
b=2005
c=a | b
The two variables a and b are assigned specific numbers.
Then another variable c is created, which is assigned the values ​​of a and b using the OR operation.
Because the variables are displayed one after the other, the individual bit positions can be easily compared.

The simplest application of this calculator is, of course, converting a number from one representation to another.
To do this, simply enter the number in any desired number representation.
All three number representations used here are then always displayed.

Conversion from decimal to binary

The conversion is easily done by hand,
by repeatedly dividing the decimal number or the previous division result by 2
and noting the remainder each time.
The process ends when the division result is 0.
The remainders, read from right to left, then form the desired binary representation.

Example: given the decimal number 371
371:2 = 185 remainder 1
185:2 = 92 remainder 1
92:2 = 46 remainder 0
46:2 = 23 remainder 0
23:2 = 11 remainder 1
11:2 = 5 remainder 1
5:2 = 2 remainder 1
2:2 = 1 remainder 0
1:2 = 0 remainder 1
Thus, the binary number is obtained 101110011

The individual nibbles (possibly padded with zeros from the left) can then be easily assigned to hexadecimal digits.
This yields the hexadecimal number 173.