Skip to main content

Command Palette

Search for a command to run...

Fixed-Point Binary Representation of Numbers

Updated
5 min read
M

A passionate sophomore student from BAIUST, Cumilla. Eager to learn about new things.

Introduction to Binary Representation of Numbers-

Introduction to Binary Representation of Numbers involves understanding how computers store and manipulate numerical data. Here are the key points:

  1. Binary System: Computers use the binary system, which consists of only two digits: 0 and 1. Each digit is called a bit (binary digit).

  2. Bit and Byte: A single binary digit (bit) can represent two states (0 or 1). Eight bits together form a byte, which is the basic unit of storage in computers.

  3. Binary Number Representation: In binary, numbers are represented using powers of 2. For example:

    • '$0_{\text{b}} = 0$'

    • '$1_{\text{b}} = 1$'

    • '$10_{\text{b}} = 2$'

    • '$11_{\text{b}} = 3$'

    • '$100_{\text{b}} = 4$'

    • and so on.

  4. Binary to Decimal Conversion: Converting binary numbers to decimal (base 10) involves multiplying each binary digit by ( 2^n ), where ( n ) is the position from right to left (starting at 0 for the rightmost bit).

  5. Applications: Binary representation is fundamental in computer architecture for data storage, arithmetic operations, and logic circuits. It forms the basis of all data processing in digital computers.

Understanding binary representation helps in grasping how computers store and manipulate data at the most basic level, forming the foundation for more complex topics like computer architecture and programming.

Converting a real number (like a decimal number) to its equivalent binary representation involves different techniques depending on whether the number is an integer or a fraction. Here’s how we can approach each case:

Converting an Integer to Binary:

  1. Divide and Remainder Method:

    • Divide the integer by 2.

    • Record the remainder (either 0 or 1).

    • Repeat the division with the quotient until the quotient becomes 0.

    • The remainders, read from last to first, give you the binary representation.

Example: Convert 13 to binary:

  • 13 ÷ 2 = 6 remainder 1

  • 6 ÷ 2 = 3 remainder 0

  • 3 ÷ 2 = 1 remainder 1

  • 1 ÷ 2 = 0 remainder 1

So, ( 13_{10} ) (decimal) converts to ( 1101_{2} ) (binary).

Converting a Fractional Number to Binary:

  1. Fractional Conversion:

    • Multiply the fractional part by 2.

    • The integer part of the result is the next binary digit.

    • Repeat with the fractional part of the result until the fractional part becomes 0 or until you reach the desired precision.

Example: Convert ( 0.625 ) to binary:

  • ( 0.625 \times 2 = 1.25 ) → 1 (integer part)

  • ( 0.25 \times 2 = 0.5 ) → 0 (integer part)

  • ( 0.5 \times 2 = 1.0 ) → 1 (integer part)

So, ( 0.625_{10} ) (decimal) converts to ( 0.101_{2} ) (binary).

Combined Example:

To convert a mixed number, such as ( 13.625 ), first convert the integer part (13) using the integer method and then convert the fractional part (0.625) using the fractional method.

These methods provide a straightforward way to convert real numbers from decimal (base 10) to binary (base 2) representation.

For any integer, the algorithm for finding the binary equivalent is given in the flow chart

We can implement this algorithm using C language-

#include <stdio.h>

// Function to convert integer part to binary
void ToBinary(int num){
    if(num==0){
        printf("0");
        return;
    }
    int binary[32]; // Array to store binary digits
    int in=0;

    while(num>0){
        binary[in++]=num%2;
        num=num/2;
    }

    // Print binary digits in reverse order
    for(int i=in-1; i>=0; i--) {
        printf("%d", binary[i]);
    }
}

// Function to convert fractional part to binary
void Frac(double fraction){
    if(fraction==0.0){
        printf(".0");
        return;
    }

    printf(".");
    while(fraction>0.0) {
        fraction=fraction*2;
        if(fraction>=1.0) {
            printf("1");
            fraction=fraction-1.0;
        }else{
            printf("0");
        }
    }
}

// Function to convert decimal to binary
void decimalToBinary(double num){
    // Separate integer and fractional parts
    int integerPart=(int)num;
    double fractionalPart=num-integerPart;

    // Convert integer part to binary
    ToBinary(integerPart);

    // Convert fractional part to binary
    Frac(fractionalPart);
}

int main() {
    double decimalNumber;

    // Input the decimal number
    printf("Enter a decimal number: ");
    scanf("%lf", &decimalNumber);

    // Check for negative numbers
    if (decimalNumber<0){
        printf("Binary representation of negative numbers is not supported.\n");
        return 1;
    }

    // Print the binary representation
    printf("Binary representation: ");
    decimalToBinary(decimalNumber);
    printf("\n");

    return 0;
}

Writing a Fixed-Point Binary Number in Given Word Length*.*

Introduction

A fixed-point binary number may need to be stored in a given number of bits called the word length. Some of the bits in the word would be used for the integer part and the rest of them for the fractional part. In addition, a bit would also be used to signify the sign of the number. This is best illustrated through an example.

Given that a fixed-point positive binary number is stored in eight bits word length, where the first six bits are used for the integer part and the next two for the fractional part, how would the (13.875)10 be stored? We are given that (13.875)10=(1101.111)2

Since there are six bits for the integer part and two for the fractional part, we will first rewrite the number as

(13.875)10=(1101.111)2≈(001101.11)2

This occupies the right number of bits for the integer part (6 bits6 bits) and the fractional part (2 bits2 bits). We write 11011101 as 001101001101 to occupy the 66 places without changing it value. We approximate the fractional part .111.111 to .11.11 as we have only 22 bits for it.

A fixed-point format with 6 bits for the integer part of the number and 2 bits for the fractional part, following the radix point.

In this day we've learned about

  1. Enumerate different bases of number systems and be able to convert base-2 numbers to base-10 numbers.

  2. Convert a base-10 number to a base-2 number in a fixed format.

  3. Write a base-2 number in a given word length.