Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 7

Binary equivalent of a number from 0 to 255

CSimple numeric problemsJNTUH R22 · CS108ES

Aim

To write a C program that reads a number between 0 and 255 and prints its binary equivalent as 8 bits, rejecting numbers outside that range.

Algorithm

  1. Read the number n.
  2. If n is less than 0 or greater than 255, print a message and stop.
  3. Repeat 8 times: store n % 2 (the next bit, starting from the rightmost) in an array, then divide n by 2.
  4. Print the 8 stored bits in reverse order, from the last one stored to the first, so the leftmost bit comes first.

Program

C
#include <stdio.h>

#define BITS 8

int main(void) {
    int n;
    int bits[BITS];

    printf("Enter a number between 0 and 255: ");
    if (scanf("%d", &n) != 1) {
        printf("Invalid input\n");
        return 1;
    }
    if (n < 0 || n > 255) {
        printf("The number must be between 0 and 255\n");
        return 1;
    }

    int value = n;
    for (int i = 0; i < BITS; i++) {
        bits[i] = value % 2;    /* remainder is the next bit from the right */
        value = value / 2;
    }

    printf("Binary equivalent of %d is ", n);
    for (int i = BITS - 1; i >= 0; i--) {
        printf("%d", bits[i]);
    }
    printf("\n");
    return 0;
}

Output

Number 10

Output 1
Enter a number between 0 and 255: 10
Binary equivalent of 10 is 00001010

Number 200

Output 2
Enter a number between 0 and 255: 200
Binary equivalent of 200 is 11001000

Number out of range

Output 3
Enter a number between 0 and 255: 300
The number must be between 0 and 255

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

Dividing by 2 again and again gives the bits from the rightmost (least significant) to the leftmost, so they are stored in an array and printed in reverse. For 200: 200, 100, 50, 25, 12, 6, 3, 1 give remainders 0, 0, 0, 1, 0, 0, 1, 1, which read backwards is 11001000.

Every number from 0 to 255 fits in 8 bits (one byte), because 2⁸ = 256. Running the loop exactly 8 times prints the leading zeros too, so 10 is shown as 00001010.

The same bits can be found with bitwise operators: bit i of n is (n >> i) & 1.

Common mistakes

  • Printing each remainder as soon as it is found. This prints the bits in reverse order (10 would show as 01010000). Store them and print from the last to the first.

  • Stopping the loop when the number becomes 0. That drops the leading zeros, so 10 prints as 1010 instead of the 8-bit 00001010.

  • Changing n inside the loop and then printing n in the result line, which shows 0. Work on a copy of the number.

  • Not checking the range. A number such as 300 needs 9 bits, so 8 bits would give a wrong answer.

Viva questions

Try to answer each one aloud before you open it.

  • How do you convert a decimal number to binary by hand?

    Divide the number by 2 repeatedly and write down the remainders. Reading the remainders from the last to the first gives the binary number.

  • Why is the range 0 to 255?

    8 bits can represent 2⁸ = 256 different values, which are 0 to 255 for an unsigned number. 255 is 11111111.

  • What is the binary equivalent of 10?

    00001010 in 8 bits, because 10 = 8 + 2.

  • How can you get the i-th bit of a number using bitwise operators?

    Shift the number right by i places and AND it with 1: (n >> i) & 1.

  • Why are the bits stored in an array?

    The division method gives the rightmost bit first, but the binary number must be printed leftmost bit first. The array lets the program print them in reverse.

Learn the concepts

All Programming for Problem Solving Lab experiments →