Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Decimal to Binary Conversion

 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).

Source Code:-

#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
  int n,c,k;
  cout<<"Convert Decimal To Binary No. by-Tarun Rawat\n";
  cout<<"Enter an Integer in decimal number system : ";
  cin>>n;
  cout<<"Binary number system is : ";
  for (c = 31; c >= 0; c--)
  {k = n >> c;
    if (k & 1)
      cout<<"1";
    else
      cout<<"0";
  }
    cout<<"\n";
getch();

}

No comments:

Post a Comment

Blogger Widgets

Find Us On Google, Just type - way2cplusplus -

If you have any questions on implementing or understanding this C++ Program , shoot me a comment and I'll be glad to help! :) :)