Share Your Experience: Take the 2024 Developer Survey

Tag Info

For challenges about binary, or the base 2 number system.

Binary is a number system with only two symbols, 0, and 1. Because computers work in binary, it is important that computer programmers know how to use binary.

Decimal (i.e. "normal" numbers) is Base 10. Base 10 works with powers of ten, and uses the symbols $$\\{0,1,2,3,4,5,6,7,8,9\}\$$. Our numbers are of the form $$\...d_3d_2d_1d_0\$$ where the $$\d_i\$$s represent digits. The $$\i\$$ represents the power of then of the digit. For example, $$\d_0\$$ is the ones digit; the value of that digit is $$\d_0 \times 10^0\$$. In general, the value of $$\d_i = d_i \times 10^i\$$. The value of a number is $$\\sum_{i=0}^\infty d_i \times 10^i\$$. Note that when we write numbers, we omit the leading zeros, so $$\...00001\$$ is written as $$\1\$$.

Binary works similar to decimal, except every $$\10\$$ is replaced with $$\2\$$ and only uses the symbols $$\\{0,1\}\$$. The value of a binary number is $$\\sum_{i = 0} ^ \infty b_i \times 2^i\$$ where $$\b_i\$$ is the bit (binary digit) at the $$\i\$$th place.

Useful binary operators:

• &, the bitwise and. This operator looks at the two numbers bit by bit and if and only if both numbers have a 1 at the same bit, the output will also have a 1 at that bit. For example:

  100100100100100 & 101010101010101 is

100100100100100
& 101010101010101
------------------
100000100000100

• |, the bitwise or. This operator looks at the two numbers bit by bit and if at least one number's bit is 1, the output will have a 1 at that point. Example:

  100100100100100 | 101010101010101 is

100100100100100
| 101010101010101
------------------
101110101110101

• ^, the bitwise xor. This operator looks at the two numbers bit by bit and if and only if both numbers' bit are not the same, the output will have a 1 at that bit. Example:

  100100100100100 ^ 101010101010101 is

100100100100100
^ 101010101010101
------------------
001110001110001

• ~, the bitwise not. This unary operator flips the bits of a number. Example:

  ~100001000100101 = 011110111011010

• <<, >>, >>> bitshifts. These shift a number left or right, depending on the number to the right of the operator. Bits shifted past the edge of the number are lost. << is just a fancy way (much faster way) of multiplying an integer by 2number right of operator. For example:

  100100100100100 << 5 = 010010010000000

100100100100100
001001001001000
010010010010000
100100100100000
001001001000000
010010010000000


>> and >>> differ only for negative integers. The both divide the number by 2number right of operator. >>> works like so:

  100100100100100 >>> 5 = 000001001001001

100100100100100
010010010010010
001001001001001
000100100100100
000010010010010
000001001001001


>>s only difference is with signed integers. Your computer represents negative numbers in two's complement. This means that for negative numbers, we start counting at all 1s for negative one, and count upwards by using 0s (please see wikipedia article for better explanation). With >>, the leftmost bit (ie the sign bit) is used to fill the rest of the number. Example:

  100100100100100 (-14044) >>> 5 = 111111001001001

100100100100100
110010010010010
111001001001001
111100100100100
111110010010010
111111001001001