Digital computers store information as sequences of binary digits, or bits, which take on the value or . Each bit corresponds to a power of two. For example, the eight-bit pattern represents the decimal value because and ; summing these yields . Bitwise operations act directly on these representations, enabling low-level manipulations that are extremely efficient.
Consider two unsigned integers and . Many programming languages expose operators that work on their bit patterns. This page lets you explore six fundamental operations: AND, OR, XOR, NOT, left shift, and right shift. The first three combine corresponding bits of two numbers to yield a result, while NOT inverts each bit of a single number. Shifts move bits left or right, effectively multiplying or dividing by powers of two. Understanding these operations is essential for tasks like setting flags, packing data, crafting cryptographic primitives, or optimizing arithmetic.
At the heart of bitwise logic lies the truth table. For AND, a bit in the result is only when the corresponding bits of both inputs are . OR yields if either input has a . XOR, short for “exclusive OR,” produces when exactly one input bit is . The following table summarizes these behaviours:
A | B | A AND B | A OR B | A XOR B |
---|---|---|---|---|
0 | 0 | 0 | 0 | 0 |
0 | 1 | 0 | 1 | 1 |
1 | 0 | 0 | 1 | 1 |
1 | 1 | 1 | 1 | 0 |
NOT, denoted by a tilde in languages like C (~A
), simply flips each bit: becomes and becomes . When numbers use two’s complement representation, NOT of equals .
Left shift moves bits toward the most significant end, inserting zeros on the right. Shifting left by places multiplies it by as long as no significant bits overflow the word size. Right shift has two flavors. Logical right shift introduces zeros on the left, effectively dividing by . Arithmetic right shift preserves the sign bit when handling signed integers, ensuring that negative numbers remain negative. The calculator presented here uses JavaScript’s unsigned right shift to maintain predictable results.
Because humans find long binary strings cumbersome, programmers often use decimal or hexadecimal notation. Hexadecimal digits base sixteen range from to , commonly written as – and –. Each hex digit corresponds to four bits, simplifying conversions. For example, equals binary . This page accepts input in decimal, binary, or hexadecimal. Internally, each string is converted to a JavaScript BigInt
with parseInt()
, then operations are performed using BigInt
operators.
Let and denote the -th bits of two numbers. Bitwise AND can be expressed as , treating bits as elements of {0,1}. XOR corresponds to addition modulo two: . Left shift by multiplies by , i.e., .
Bitwise operations appear throughout computing. In graphics programming, individual color channels are packed into a single integer; masking with AND and shifting extracts each channel. Network protocols define bit fields to represent flags like SYN or ACK in TCP headers. File permissions in Unix systems rely on bit patterns such as rwxr-xr-x
; a bitwise OR combines permissions, and AND verifies them. Cryptographic algorithms often mix XOR, shifts, and additions to achieve diffusion and confusion. For example, the classic Feistel structure uses XOR to mix subkeys with data, while modern stream ciphers combine shift registers with XOR gates.
Most programming languages encode signed integers using two’s complement. In this scheme the most significant bit denotes sign, and negative numbers are obtained by inverting all bits of the positive value and adding one. When applying bitwise operations to signed values, it is important to consider how the sign bit behaves. Left shifting a signed integer may overflow into the sign bit, flipping the sign unexpectedly. Logical right shift fills vacated bits with zeros, potentially changing a negative number to positive. Arithmetic right shift replicates the sign bit, keeping negative numbers negative. JavaScript’s bitwise operators implicitly convert numbers to 32-bit signed integers. To avoid truncation, this calculator uses the BigInt
-based versions &
, |
, ^
, ~
, <<
, and >>
, maintaining full precision.
Suppose and , entered in decimal. In binary these correspond to and . The AND result is (), OR yields (), and XOR gives (). NOT of assumes an infinite leading zero representation and flips all bits, effectively computing . Left shifting by two positions multiplies it by , producing . Right shifting by one divides by , giving .
The ability to reason about bits permeates computer science. Boolean algebra, developed in the nineteenth century by George Boole, provides a rigorous foundation for the logical operations implemented by modern hardware. Claude Shannon recognized that electrical circuits could represent Boolean expressions, launching the field of digital electronics. Microprocessors integrate billions of transistors implementing these operations at staggering speed. Algorithms from compression to cryptography leverage bitwise manipulations to achieve efficiency and security. Mastering these concepts equips programmers to optimize performance, understand data formats, and debug low-level issues.
Below is a summary table showing how different operations transform the example numbers () and ().
Operation | Result (Decimal) | Result (Binary) |
---|---|---|
AND | 1 | 0001 |
OR | 7 | 0111 |
XOR | 6 | 0110 |
NOT (5) | -6 | ...11111010 |
5 << 3 | 40 | 00101000 |
5 >> 1 | 2 | 0010 |
This calculator operates entirely in your browser. You can save the HTML file and run it offline to experiment with bitwise operations, making it a handy reference for students, hobbyists, and professionals alike. By observing how numbers change under each operation, you build intuition that carries over to all programming languages. Whether you are configuring network masks, crafting cryptographic puzzles, or optimizing shaders, a solid grasp of bit-level manipulation will serve you well.
Calculate total pay including shift differential for evening, night, or weekend work.
Compute the phase shift and time delay observed in a rotating Sagnac interferometer from its geometry, rotation rate, and light wavelength.
Convert Raman shift values to scattered wavelengths for spectroscopy experiments.