Bit, byte and the conversion from/to decimal

A bit can have only one of two values: 0 or 1. The term bit is a shorted form of binary digit. The two values (0 and 1) can also be interpreted as logical values (true/false, yes/no), algebraic signs (+/?) and so on. A group of eight bits is commonly called one byte, but historically the size of the byte is not strictly defined.

The symbol for bit, as a unit of information, is either simply bit (ISO/IEC standard 80000-13 (2008)) or lowercase b (IEEE 1541 Standard (2002)). In contrast, the upper case letter B is the standard and customary symbol for byte. While the term byte might be ambiguous, you might findpicture the term octet as a representation of 8 bits (used in the representation of IP addresses and network protocol parameters).

Computers usually manipulates bits in groups of a fixed sizes, conventionally named words. Like the byte, the number of bits in a word also varies with the hardware design, and is typically between 8 and 80 bits, or even more. Nowadays, the most common word size are of 32 or 64 bits.

The lack of human capabilities to think in binary, led to different mapping mechanism: byte, ASCII, hexadecimal and so on. As such, when it comes to a conversion for human use, this is what 8 bits can represent:

  • 00000000 – decimal 0
  • 00000001 – decimal 1
  • 00000010 – decimal 2
  • 00000011 – decimal 3
  • 00000100 – decimal 4
    ……..
  • 11111110 – decimal 254
  • 11111111 – decimal 255

Who said that math is fun? Let’s see several methods that would allow us to convert a decimal into a binary and a binary back into a digital. And let’s start by saying that a decimal number has digits that go from 0 to 9 and that the bytes have the highest value bit at the left and the lowest value at the right (if I lost you already, is the exact same way as a decimal number). Now, each bit in a byte is referred using it’s index number:

  • bit 7 – index: 7 (MSB)
  • bit 6 – index: 6
  • bit 5 – index: 5
  • bit 4 – index: 4
  • bit 3 – index: 3
  • bit 2 – index: 2
  • bit 1 – index: 1
  • bit 0 – index: 0 (LSB)

where bit 0 is is the lowest value bit (Lowest Significant Bit – LSB) and bit 7 is the highest value bit (Most Significant Bit – MSB). Let’s see now how we can convert the byte 10101101 into a decimal number. First of all, this byte is represented by position – you will move from right to left and increase the multiplier by the power of two (2). This multiplier is used as the binary format is using the base-2 notation.

  • 1st digit from right multiplier 20 = 1
  • 2nd digit from right multiplier 21 = 2
  • 3rd digit from right multiplier 22 = 4
  • 4th digit from right multiplier 23 = 8
  • 5th digit from right multiplier 24 = 16
  • 6th digit from right multiplier 25 = 32
  • 7th digit from right multiplier 26 = 64
  • 8th digit from right multiplier 27 = 128

Now let’s get back to our binary value – 10101101 – and let’s apply the same logic.

  • 1st digit represents the number of 20‘s => 1 * 20
  • 2nd digit represents the number of 21‘s => 0 * 21
  • 3rd digit represents the number of 22‘s => 1 * 22
  • 4th digit represents the number of 23‘s => 1 * 23
  • 5th digit represents the number of 24‘s => 0 * 24
  • 6th digit represents the number of 25‘s => 1 * 25
  • 7th digit represents the number of 26‘s => 0 * 26
  • 8th digit represents the number of 27‘s => 1 * 27

Considering the above, our digital representation of our byte is the sum of all the values:

(1 * 20) + (0 * 21) + (1 * 22) + (1 * 23) + (0 * 24) + (1 * 25) + (0 * 26) + (0 * 27) =
= 1 + 0 + 4 + 8 + 0 + 32 + 0 + 128 =
= 173

Now that we have seen how to convert the byte into a digit, let’s see how we can covert the number 173, into a byte or a binary format. For this, on each bit position you need to see if the bit adds to the total value or if it doesn’t. This time, we will start with the highest bit position value and we will use the division with remainder technique.

  • Can I divide the full number 174 by 128?
  • Yes, the whole part is 1 and the remainder is 36.

  • Can I divide the remainder 46 by 64?
  • Yes, with the whole part 0 and the remainder 36.

  • Can I divide the remainder 46 by 32?
  • Yes, with the whole part 1 and the remainder 14.

  • Can I divide the remainder 14 by 16?
  • Yes, with the whole part 0 and the remainder 14.

  • Can I divide the remainder 14 by 8?
  • Yes, with the whole part 1 and the remainder 6.

  • Can I divide the remainder 6 by 4?
  • Yes, with the whole part 1 and the remainder 2.

  • Can I divide the remainder 2 by 2?
  • Yes, with the whole part 1 and the remainder 0.

  • Can I divide the remainder 0 by 1?
  • Yes, with the whole part 0 and the remainder 0.

    Once I got the remainder 0, I can easily obtain the byte associated with the digit 174, by having the whole part concatenated from right to left. As such, the resulted byte is 10101110.

    Both methods can be scaled up for as many bytes, binary digits or digital digits that you might need.