Q:

Decimal to binary in C

0
Decimal to binary in C

Decimal to binary in C to convert an integer from decimal number system (base-10) to binary number system (base-2). The size of an integer is assumed to be 32 bits. We use the bitwise operator "AND" to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a for loop and bitwise AND the number obtained with 1(one) if the result is 1, then that bit is one otherwise zero (0).

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <stdio.h>

int main()
{
  int n, c, k;

  printf("Enter an integer in decimal number system\n");
  scanf("%d", &n);

  printf("%d in binary number system is:\n", n);

  for (c = 31; c >= 0; c--)
  {
    k = n >> c;

    if (k & 1)
      printf("1");
    else
      printf("0");
  }

  printf("\n");

  return 0;
}

output:

Enter an integer in decimal number system

100

100 in binary number system is:

00000000000000000000000001100100

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now