Skip to content

Instantly share code, notes, and snippets.

@satveersm
Last active August 29, 2015 14:04
Show Gist options
  • Save satveersm/50437401d4c72caf9e4e to your computer and use it in GitHub Desktop.
Save satveersm/50437401d4c72caf9e4e to your computer and use it in GitHub Desktop.
PAGE - 2
//DEC TO BCD
//BCD format each deciaml digit hold 4 bits 0->0001
//Bitset used here
//std::bitset<32> b(num)
#include<iostream>
using namespace std;
//Unsigned int to bcd
unsigned int uinttobcd(unsigned int i)
{
unsigned int res = 0;
int div = 1;
while(i/div > 10)
{
div = div*10;
}
while(i != 0)
{
unsigned int x = i/div;
res = res<<4;
res = res | x;
i = i%div;
div = div/10;
}
return res;
}
int main()
{
unsigned int num = 9999;
unsigned int res = uinttobcd(num);
std::bitset<32> p(num);
cout<<"DEC = "<<p<<endl;
std::bitset<32> x(res);
cout<<"BCD = "<<x<<endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment