How do computers convert data into binary form?
Introduction
We all are very aware of the fact that a computer is a digital device. Being a digital device it only accepts the input in binary form and produces output in binary form. Therefore, the data that will be used for the operation should be available in binary form. The data could either be a number or characters/special symbols. So let us talk about the different ways to convert the numbers into binary form- first and then we will move towards the characters/special symbols conversion.
Numbers Conversion
The numbers could either be Fixed-Point or Floating-Point.
Talking about Fixed-Point, it could again be Unsigned or Signed.
Unsigned Fixed-Point Number
Unsigned fixed-point numbers are a way of representing non-negative, fractional numbers in a computer system using a fixed number of bits. In contrast to floating-point numbers, which have a separate exponent and mantissa, fixed-point numbers use a fixed point of reference to represent the decimal point, and the number of bits allocated to the integer and fractional parts of the number is pre-determined.
Unsigned fixed-point numbers are called "unsigned" because they represent only non-negative numbers, i.e., they do not include a sign bit. They are also referred to as "fixed-point" because the binary point or decimal point of the number is fixed, unlike a floating-point number where the binary point can move based on the exponent.
The fixed point can be placed at any position in the binary representation of the number, depending on the requirements of the application. For example, if the fixed point is placed after the first four bits, then the first four bits represent the integer part, and the remaining bits represent the fractional part of the number. The resolution or precision of the number (i.e., the size of the smallest representable increment) is determined by the number of bits allocated to the fractional part.
Unsigned fixed-point numbers are commonly used in digital signal processing (DSP) applications, where they provide a simple and efficient way to represent fractional values without the overhead of floating-point arithmetic. However, they have limited range and precision compared to floating-point numbers and require careful design and scaling to avoid overflow or underflow errors.
How to convert Unsigned Fixed-Point Numbers into binary?
- Division by 2: This is the most common method for converting unsigned decimal integers to binary. To do this, you repeatedly divide the decimal number by 2, recording the remainder at each step, until the quotient is zero. The binary representation of the decimal number is the sequence of remainders, written in reverse order.
Bit-shifting: Another way to convert unsigned decimal integers to binary is by bit-shifting. To do this, you start with a binary number with all zeroes, and then shift the bits to the left, one at a time, based on the powers of 2. At each step, if the current power of 2 is less than or equal to the decimal integer, you set the corresponding bit in the binary number to 1, and subtract the power of 2 from the decimal integer.
Hexadecimal conversion: You can also convert unsigned decimal integers to binary by first converting the decimal number to hexadecimal, and then converting each hexadecimal digit to its corresponding binary value. To do this, you divide the decimal number by 16, recording the remainder at each step until the quotient is zero. The hexadecimal representation of the decimal number is the sequence of remainders, written in reverse order. Then, each hexadecimal digit can be converted to its corresponding 4-bit binary value.
Octal conversion: Octal conversion is similar to hexadecimal conversion, but you divide the decimal number by 8 instead of 16. The octal representation of the decimal number is the sequence of remainders, written in reverse order. Each octal digit can be converted to its corresponding 3-bit binary value.
Signed Fixed-Point Numbers
A signed fixed-point number is a way of representing both positive and negative fractional numbers in a computer system using a fixed number of bits. Like unsigned fixed-point numbers, they use a fixed point of reference to represent the decimal point, and the number of bits allocated to the integer and fractional parts of the number is pre-determined. However, unlike unsigned fixed-point numbers, signed fixed-point numbers include a sign bit to represent negative values.
In a signed fixed-point number, the sign bit is usually placed at the most significant bit position. For example, if an 8-bit signed fixed-point number is used, the sign bit is placed in the 8th-bit position, and the remaining 7 bits are used to represent the number's magnitude.
How to convert Signed Fixed-Point Numbers into binary?
Two's complement: This is the most common technique for representing signed numbers in binary. In this method, the most significant bit (MSB) is used as the sign bit. If the MSB is 0, the number is positive; if it is 1, the number is negative. To convert a signed fixed-point number into binary using two's complement, first, convert the integer and fractional parts into their unsigned binary representation. Then, if the number is negative, take the two's complement of the entire binary representation by inverting all the bits and adding 1 to the result.
Sign-and-magnitude: In this technique, the most significant bit (MSB) is still used as the sign bit, but the remaining bits represent the magnitude of the number. The MSB is set to 0 for positive numbers and 1 for negative numbers. To convert a signed fixed-point number into binary using sign-and-magnitude, first, convert the integer and fractional parts into their unsigned binary representation. Then, set the MSB to 0 or 1 depending on the sign of the number.
Offset binary: In this technique, a fixed offset is added to the unsigned binary representation of the number to represent negative values. For example, if the offset is 1000 for an 8-bit number, then the range of values that can be represented is -8 to +7. To convert a signed fixed-point number into binary using offset binary, first, convert the integer and fractional parts into their unsigned binary representation. Then, add the offset to the binary representation if the number is negative.
Excess-K: This technique is similar to offset binary, but the offset is chosen to be half the maximum range of values that can be represented. For example, if the range of an 8-bit number is -128 to +127, the offset would be 128/2 = 64. To convert a signed fixed-point number into binary using excess-K, first, convert the integer and fractional parts into their unsigned binary representation. Then, subtract the offset from the binary representation if the number is negative.
Let us now dive into the floating point numbers.
Floating Point Numbers
A floating-point number is a way of representing real numbers (numbers that include decimal fractions) in a computer system. Unlike fixed-point numbers, which have a fixed number of digits allocated to the integer and fractional parts, floating-point numbers have a dynamic range and precision that can be adjusted to suit the requirements of the application.
A floating-point number is represented by three components:
Sign: A single bit that represents the sign of the number, indicating whether it is positive or negative.
Exponent: A fixed number of bits that represent the magnitude of the number, typically in scientific notation. The exponent determines the scale of the number, indicating where the decimal point should be placed.
Mantissa: Also known as the significand or fraction, this component represents the significant digits of the number. The mantissa is a variable-length sequence of bits that contains the digits of the number, including the decimal point.
How to convert floating point numbers into binary?
The IEEE 754 standard is the most widely used technique for converting floating-point numbers into binary in computer systems. It defines the format for representing floating-point numbers in binary using three components: sign, exponent, and mantissa. This format allows the representation of a wide range of real numbers with varying precision and range. The conversion process involves determining the sign bit, converting the integer and fractional parts of the number into binary, normalizing the binary representation, determining the exponent bias, calculating the exponent, and combining all the components into the final binary representation of the floating-point number.
Characters/Special Symbols
Here are several different ways to convert characters/special symbols into binary in computers:
ASCII encoding: ASCII (American Standard Code for Information Interchange) is a character encoding standard that uses a 7-bit binary code to represent characters. Each character is assigned a unique 7-bit binary code, which can be converted to binary form.
Unicode encoding: Unicode is a character encoding standard that uses a variable-length binary code to represent characters from different scripts and languages. Each character is assigned a unique binary code, which can be converted to binary form.
UTF-8 encoding: UTF-8 is a variable-length character encoding standard that can represent any character in the Unicode standard. It uses 8-bit code units and can represent characters using one, two, three, or four code units, depending on the character.
Binary-coded decimal (BCD) encoding: BCD is a method of encoding decimal numbers using four bits for each digit. BCD encoding can be used to represent characters that are numeric in nature.
Base64 encoding: Base64 is a binary-to-text encoding scheme that represents binary data in an ASCII string format. It is commonly used to transmit binary data over channels that only support text data.