Programmers zero a register by combining it with itself
In short: To set a processor register to zero, assembly programmers and compilers often skip loading a zero at all. They XOR the register with itself, which always yields nothing, and on many chips it takes fewer cycles and less memory. It is a small example of how much work gets done by flipping individual bits.
Bitwise operations treat a number as a row of ones and zeros and act on each position separately. NOT flips every bit. AND gives 1 only where both inputs have a 1; OR gives 1 where either does; XOR gives 1 only where the two inputs differ. On cheap, simple processors these are much quicker than division, several times quicker than multiplication and sometimes faster than addition. Modern chips have closed most of that speed gap, but bitwise instructions still tend to draw less power.
Each has a practical job. AND acts as a mask: combine a value with a pattern holding a single 1, and a non-zero answer reveals whether that bit was set, the way masking tape shields parts of a surface. Checking only the lowest bit tells you instantly whether a number is odd or even; 6 AND 1 gives zero, so 6 is even. OR switches chosen bits on, and XOR toggles them. Packing many true-or-false flags into the bits of one register saves memory, a trick used heavily in low-level code.
NOT has a neat visual side. For 8-bit unsigned values it amounts to 255 minus the number, so applying it to every pixel of a greyscale image produces a photographic negative. In two's complement arithmetic, the scheme most computers use for signed numbers, NOT x equals minus x minus one.
Shifts slide bits left or right within a fixed-width register. Shifting left by n places multiplies by 2 to the power n, barring overflow, and an arithmetic right shift copies the sign bit in so negative numbers stay negative. Rotations wrap bits from one end around to the other so nothing is lost, which makes them a staple of cryptography. With two inputs there are exactly 16 possible logical operations; with three, 256.
Opens the Intellect+ feed with this card first. Swipe up for the next one.
Sources
- Summary by Intellect · source: Bitwise operation
To cite this card, link to https://intellectplus.co.uk/app/science-bitwise-operation.
How we source, check and correct cards: Editorial standards.
General information, not professional advice. Spotted an error? Flag the card or see corrections and complaints.
Intellect Plus is a visual educational discovery platform for exploring fascinating facts and ideas. Brain Food, Not Brain Rot. What is Intellect Plus?
Related
More in Binary · More in Technology · Latest · A–Z · All topics