Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Decimal to Binary Conversion Efficent.
- //using Bitwise AND and RightShift operators to achieve maximum efficeny.
- //Written by HaseeB Mir (haseebmir.hm@gmail.com)
- //Dated : 25/02/2017
- #include<stdio.h>
- #define BYTE 8
- #define WORD 16
- #define DWORD 32
- #define QWORD 64
- int main()
- {
- long long num; //Long DataType to store big numbers.
- int bits; //No. of Bits to represent
- printf("Enter Decimal Number : ");
- scanf("%ld",&num);
- printf("Enter No. of Bits to represent : ");
- scanf("%d",&bits);
- //Rounding bits according to BYTE,WORD etc (MAX 64bit representation).
- (bits <= BYTE) ? bits = BYTE : (bits > BYTE && bits <= WORD) ? bits = WORD:
- (bits > WORD && bits <= DWORD) ? bits = DWORD : (bits > DWORD && bits <= QWORD) ? bits = QWORD :
- (bits > QWORD) ? bits = QWORD : bits;
- printf("\nBits = %d\n",bits);
- printf("Binary : \t");
- for (bits = bits - 1; bits >= 0; bits--)
- printf("%d",( (num >> bits) & 1));
- printf("\n");
- return 0;
- }
Add Comment
Please, Sign In to add comment