How To Write a C Program To Convert Decimal to Binary Using Bitwise AND Operator in C Programming Language ?
Solution For C Program To Convert Decimal to Binary Using Bitwise AND Operator:
#include<stdio.h> #include<conio.h> void main() { unsigned int mask; clrscr(); printf("Memory Required : %d butes",sizeof(mask)); getch(); }Output :Memory Required : 2 bytes
Above program is just to know the size of integer variable in C Programming (Borland C/C++ Compiler.) Integer number can be represented by 16 bits.
To convert the Decimal Number into Binary , Check First MSB bit of number , if it is 1 then display ’1′ otherwise display ’0′.
#include<stdio.h> #include<conio.h> void binary(unsigned int); // Prototype Declaration void main() { unsigned int num; printf("Enter Decimal Number : "); scanf("%u",&num); binary(num); // Function Call getch(); } //======================================================== void binary(unsigned int num) { unsigned int mask=32768; //mask = [1000 0000 0000 0000] printf("Binary Eqivalent : "); while(mask > 0) { if((num & mask) == 0 ) printf("0"); else printf("1"); mask = mask >> 1 ; // Right Shift } }Output :Enter Decimal Number : 10 Binary Eqivalent : 0000000000001010
Explanation of Decimal to Binary Program :
In this program we have accepted the decimal number using following lines -
printf("Enter Decimal Number : "); scanf("%u",&num);
Inside the function we have initialized mask variable with 32768.
unsigned int mask = 32768;
i.e Binary representation of the number is 1000 0000 0000 0000
mask = 1000 0000 0000 0000
Now inside iteration we need to follow same steps : If result of mask is Zero then Print 0 else Print 1.
if((num & mask) == 0 ) printf("0"); else printf("1");
Consider the first iteration Masking will be like this -
1000 0000 0000 0000 0000 0000 0000 1010 & ---------------------- 0000 0000 0000 0000
Now right shift mask by 1 so that new mask variable will contain following value -
0100 0000 0000 0000 - (Decimal Equivalent : 16384)
As mask variable is not equal to 0 so once again while loop will be executed and in the next iteration of while loop following numbers will be ANDed.
0100 0000 0000 0000 0000 0000 0000 1010 & ---------------------- 0000 0000 0000 0000
Similarly, In each iteration we are shifting mask variable by 1 to right and ANDing the mask with number.
Algorithm for Decimal to Binary using Bitwise Operator :
- Initialize ‘mask’ variable with 32768 [1000 0000 0000 0000]
- Perform ANDing of two number’s (i.e Given variable and Mask variable )
- Check whether the Result of ANDing is 0 or not , if Yes Display 0 otherwise Display 1
- Right shift mask variable by 1 [0100 0000 0000 0000]
- Now check for Second bit , whether it is 0 or 1
- Goto step 3 until ‘mask’ becomes Zero [ 0000 0000 0000 0000 ]
Why We are right shifting Mask Variable ?
We have kept given number as it is but in each iteration we have right shifted given number by 1 and masked it with original number. Main purpose of doing this is to check bit status of all the bits of given number.
In the 13th iteration mask variable will be -
In the 13th iteration mask variable will be -
Mask Variable : 0000 0000 0000 1000 Num Variable : 0000 0000 0000 1010 & -------------------------------------- Result of AND : 0000 0000 0000 1000
Tags: C Program To Convert Decimal to Binary Using Bitwise AND Operator, c program to convert decimal to binary using array, c program to convert decimal to binary using recursion, c program to convert decimal to binary using stack, c program to convert decimal to binary using function, c program to convert decimal to binary without using array, c program to convert decimal to binary using while loop, c program to convert decimal to binary octal and hexadecimal using functions, simple c program to convert decimal to binary.
Learn More :
C Program
- Using Bash to input stuff into c program
- Difficult C Programming Questions
- Write a c program to find largest among three numbers using binary minus operator three numbers using binary minus operator
- PRINTING ASCII VALUE USING C PROGRAM
- MULTIPLICATION OF TWO MATRICES USING C PROGRAM
- FIND OUT SUM OF DIAGONAL ELEMENTS OF A MATRIX USING
- Write A C Program To Find Out Transport Of A Matrix
- Factorial of 100 in C Program
- Multiplication of large numbers in c
- Division of Large Numbers in C Program
- BINARY SEARCH USING C PROGRAM
- BINARY SEARCH THROUGH RECURSION USING C PROGRAM
- FIND FACTORIAL OF A NUMBER USING RECURSION IN C PROGRAM
- FIND GCD OF A NUMBER USING RECURSION IN C PROGRAM
- FIND SUM OF DIGITS OF A NUMBER USING RECURSION USING C PROGRAM
- FIND POWER OF A NUMBER USING RECURSION USING C PROGRAM
- REVERSE A NUMBER USING RECURSION IN C PROGRAM
- SWAP TWO VARIABLES WITHOUT USING THIRD USING C PROGRAM VARIABLE
- Write A C Program For Swapping Of Two Arrays
- SWAPPING OF STRINGS USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION FROM DECIMAL TO OCTAL USING C PROGRAM
- CONVERSION OF DECIMAL TO BINARY USING C PROGRAM
- CONVERSION OF FAHRENHEIT TO CENTIGRADE USING C PROGRAM
- C or C++ Program To Find Bonus Amount