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.

    Windows – change the port for Remote Desktop

    The steps detailed through this article would involve changes to the Windows registry. Therefore, as for any changes that are performed on a system,its advisable to create a backup first. (is not always enough to follow some steps carefully).

    1. Start the Registry Editor: Start > Run > type ‘regedit’ and press enter.

    2. Navigate to the following registry subkey: HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\TerminalServer\WinStations\RDP-Tcp\PortNumber (HKEY_LOCAL_MACHINE > System > CurrentControlSet > Control > TerminalServer > WinStations > RDP-Tcp > PortNumber)

    3. At this point you should be able to see the hexadecimal value of the current port (the default remote desktop port for a Windows machine is 3389, so you should see d3d in the port field, representing the hexadecimal conversion):







    4. On the Edit menu, click Modify and then click Decimal. This should change the port value to 3389.







    5. Type the new port number, click OK and quit Registry Editor.

    IMPORTANT: Before going to step 6) make sure that the new port is opened through the firewall.

    6. Restart the computer.

    CSF on Virtuozzo

    A installation of CSF performed on a VM (based on Virtuozzo) was no longer accepting traffic to VM. In order to correct this I created the file (/etc/csf/ – and set the following set of rules – are executed before all other statements when CSF is started:

    iptables -A INPUT -i venet0 -j ACCEPT
    iptables -A OUTPUT -o venet0 -j ACCEPT
    iptables -A FORWARD -j ACCEPT -p all -s 0/0 -i venet0
    iptables -A FORWARD -j ACCEPT -p all -s 0/0 -o venet0

    This is required to allow the traffic through your virtual ethernet device (venet0). Restart the firewall and everything should work once again as expected.

    dig, host, and nslookup

    BIND Utilities is a collection of the client side programs that are included with BIND. The BIND package includes the client side programs nslookup, dig and host. If you install BIND server, these programs will be installed automatically. In the situation when you are not looking on having the BIND server installed, but you would like to install the client side applications (dig, host, and nslookup) this can be done by installing only the bind-utils package:

    $ host
    -bash: host: command not found

    $ dig
    -bash: dig: command not found

    $ nslooup
    -bash: nslookup: command not found

    yum install bind-utils

    $ dig -v
    DiG 9.7.3-P3-RedHat-9.7.3-2.el6_1.P3.3

    InnoDB: Operating system error number 13

    I was looking tonight on having the datadir for MySQL changed from /var to /home (as the /var partition was almost full). Once this has been done, I noticed that MySQL was no longer starting and it’s log was returning the following error:

    Jan 28 22:18:35 mysqld[24760]: 101011 22:18:35  InnoDB: Operating system error number 13 in a file operation.
    Jan 28 22:18:35 mysqld[24760]: InnoDB: The error means mysqld does not have the access rights to
    Jan 28 22:18:35 mysqld[24760]: InnoDB: the directory.
    Jan 28 22:18:35 mysqld[24760]: InnoDB: File name ./ibdata1
    Jan 28 22:18:35 mysqld[24760]: InnoDB: File operation call: ‘create’.
    Jan 28 22:18:35 mysqld[24760]: InnoDB: Cannot continue operation.

    After a long investigation I noticed that this was the direct result of having SeLinux enabled on the server:

    root@dragos [~]# sestatus
    SELinux status:                 enabled

    As my customer was running CentOS on the machine, I went ahead and had SeLinux disabled, by running:

    root@root [~]# setenforce 0
    setenforce: SELinux is disabled

    The above method would allow you to disable SeLinux without having to reboot the machine. Having SeLinix disabled by editing /etc/selinux/config would require a server reboot.