haseeb_heaven

Dec_To_Bin_Efficent.c

Feb 25th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. //Decimal to Binary Conversion Efficent.
  2. //using Bitwise AND and RightShift operators to achieve maximum efficeny.
  3. //Written by HaseeB Mir (haseebmir.hm@gmail.com)
  4. //Dated : 25/02/2017
  5.  
  6. #include<stdio.h>
  7.  
  8. #define BYTE  8
  9. #define WORD  16
  10. #define DWORD 32
  11. #define QWORD 64
  12.  
  13. int main()
  14. {
  15.  
  16.  long long num; //Long DataType to store big numbers.
  17.     int bits; //No. of Bits to represent
  18.    
  19.     printf("Enter Decimal Number : ");
  20.     scanf("%ld",&num);
  21.    
  22.     printf("Enter No. of Bits to represent : ");
  23.     scanf("%d",&bits);
  24.    
  25.     //Rounding bits according to BYTE,WORD etc (MAX 64bit representation).
  26.    
  27.     (bits <= BYTE) ? bits = BYTE : (bits > BYTE && bits <= WORD) ? bits = WORD:
  28.     (bits > WORD && bits <= DWORD) ? bits = DWORD : (bits > DWORD && bits <= QWORD) ? bits = QWORD :
  29.     (bits > QWORD) ? bits = QWORD : bits;  
  30.    
  31.     printf("\nBits = %d\n",bits);
  32.    
  33.     printf("Binary : \t");
  34.     for (bits = bits - 1; bits >= 0; bits--)
  35.  printf("%d",( (num >> bits) & 1));
  36.    
  37.    
  38. printf("\n");
  39. return 0;
  40. }
Add Comment
Please, Sign In to add comment