Binary arithmetic is the foundation of modern digital computing. Since electronic circuits (transistors) are most naturally operated in two states—on or off—computers represent all data and perform all calculations using the base-2 (binary) system.
Binary addition follows the same positional logic as decimal addition, but with only two possible digits: 0 and 1.
The four basic rules are:
0 + 0 = 00 + 1 = 11 + 0 = 11 + 1 = 10 (write 0, carry 1)1 + 1 + 1 (carry) = 11 (write 1, carry 1)5 + 3 111 (carries)
101 (5)
+ 011 (3)
------
1000 (8)
While subtraction can be performed directly, modern computers use Two's Complement to represent negative numbers. This allows the CPU to perform subtraction using the same hardware circuits as addition.
5 - 3 (using 4-bit representation)5 is 01013 is 0011-3:
0011 -> 11001 -> 1101 (This is -3 in two's complement)5 + (-3):
111 (carries)
0101 (5)
+ 1101 (-3)
-------
10010 (The 5th bit is discarded in 4-bit math)
0010 (which is 2).Bitwise operations are used to manipulate individual bits within a word. They are extremely fast and essential for low-level programming (drivers, graphics, cryptography).
&): Result is 1 only if both bits are 1. (Used for masking/clearing bits).|): Result is 1 if at least one bit is 1. (Used for setting bits).^): Result is 1 only if the bits are different. (Used for toggling bits or simple encryption).~): Inverts all bits.<<): Shifts bits to the left, filling with 0s. Effectively multiplies by powers of 2.>>): Shifts bits to the right. Effectively divides by powers of 2.