 # binary

Binary is a numeral system which uses only two symbols: "0" and "1". Due to using only two symbols, it is known as a base 2 system. Each digit in binary is referred to as a bit, or a binary digit.

### Binary Digits

Also known as bits, it represents the state of two possible values: 1 or 0, true or false, yes or no, +/-, or on/off. A continous group of binary digits is commonly referred to as a bit string, a bit vector, or a single-dimensional bit array. A group of 8 of these is called one byte.

### Nibble

Half of a byte, or four bits. In networking, the symbol is typically called a semi-octet, quadbit, or quartet. The nibble has 16 possible values, a nibble can be represented by a single hexadecimal digit and called a hex digit. A full byte (Octet, often used in Networking as well.) is represented by two hexadecimal digits; therefore it is common to display a byte of information as two nibbles. Sometimes, the set of all 256 byte values represented in a 16x16 table gives easily readable hexadecimal codes for each value.

### Bytes

Consists of eight bits. The size of a byte has historically been hardware-depending and no definitive standard existed that mandated the size, sizes from 1 to 48 bits have been used. To disambiguate arbitrarily sized bytes from the common 9-bit definition RFC891 refers to an 8-bit as an octet. Those bits in an octet are usually counted numbering from 0 to 7, or 7 to 0 depending on the bit endianness.

Since bytes are to the power of two, binary encoded values range from 0 through 255 for one bytes. The C standard requires that the integral data type unsigned char must hold at least 256 different values, and is represented by at least eight bits. Various implementations of C and C++ reserve 8, 9, 16, 32, or 36 bits for the storage of a byte. In addition, the C and C++ standards require that there are no gaps between two bytes. This means every bit in memory is part of a byte.

## Counting in Binary

Counting in binary begins from the right most digit which is considered the 'least significant' digit. Since we're only dealing with 0 and 1, we only have two numbers to deal with before we have to increment. In the decimal system, we increment after 9, by bringing down the first number (the 0), and incrementing by 1 to create 10. We do this in Binary as well.

 0 1

Once we reach 1 in binary, we bring down the 0 column, and increment the next number by 1. It's much easier to think of infinite implicit zeros to the left of every number.

 1

Becomes

 10

If you are able to increment, everything after the incremented digit from the earlier binary number carries down.

 Before After 10 11

Now, we can no longer increment either number, they've reached their maximum. Now we start over, just like in decimal, we reset everything to 0, and flip and implicit 0 to 1.

 Before After 11 100

And then start over with the least significant digit. In this case, it is a 0, and can be incremented. As said, once we increment we bring down everything from the last binary digit with it. Prior we had 100, we incremented the right most digit to 1 and brought down the 10 creating 101.

 Before After 100 101

### Binary to Decimal conversion chart

 Decimal Binary 0 0 1 1 2 10 3 11 4 100 5 101 6 110 7 111 8 1000 9 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111