Decimal to binary conversion
C program to convert decimal to binary: c language code to convert an integer from decimal number system(base-10) to binary number system(base-2). Size of integer is assumed to be 32 bits. We use bitwise operators to perform the desired task. We right shift the original number by 31, 30, 29, ..., 1, 0 bits using a loop and bitwise AND the number obtained with 1(one), if the result is 1 then that bit is 1 otherwise it is 0(zero).
C programming code
Above code only prints binary of integer, but we may wish to perform operations on binary so in the code below we are storing the binary in a string. We create a function which returns a pointer to string which is the binary of the number passed as argument to the function.
C code to store decimal to binary conversion in a string
Compiler used:
GCC
Output of program:
Enter an integer in decimal number system
-1
-1 in binary number system is :
11111111111111111111111111111111
No comments:
Post a Comment