reverse division to convert decimals to dinary
-
hi everyone who reads this, i am in a digital electronics class and i decided to write a program that converts decimals to binary, i thought this was going to be simple and it was till i realized my my loop cout'ed the binary in reverse order. 731 in dec = 1011011011 and my program did it in reverse 1101101101 i used int's with modulus and division operators:
while (num)
{
rem=num%2;
num = num/2;
cout << rem;}
so i was wondering is there a simple way to reverse the output or could the division be done in reverse so the last output is first and the first last in my program? please note i am a beginner at c++ so i don't know how to do arrays as of yet and i am sure the output could be loaded into an array and printed in reverse order. thank you in advanced for any help.
-
hi everyone who reads this, i am in a digital electronics class and i decided to write a program that converts decimals to binary, i thought this was going to be simple and it was till i realized my my loop cout'ed the binary in reverse order. 731 in dec = 1011011011 and my program did it in reverse 1101101101 i used int's with modulus and division operators:
while (num)
{
rem=num%2;
num = num/2;
cout << rem;}
so i was wondering is there a simple way to reverse the output or could the division be done in reverse so the last output is first and the first last in my program? please note i am a beginner at c++ so i don't know how to do arrays as of yet and i am sure the output could be loaded into an array and printed in reverse order. thank you in advanced for any help.
You should go in the opposite way, using multiplication (or bit shif, that is faster):
#include <iostream>
using namespace std;
void main()
{
unsigned int k = 731;
unsigned int b;
const int SIZE = sizeof(k) << 3;
const unsigned int MSB = 1 << (SIZE-1);
for (b=0; b<SIZE; b++)
{
cout << (( MSB & k) ? 1 : 0);
k <<= 1;
}
cout << endl;
}Or
#include <iostream>
using namespace std;
void main()
{
unsigned int k = 731;
unsigned int b;
const int SIZE = sizeof(k) << 3;
const unsigned int MSB = 1 << (SIZE-1);
bool isLeadZero= true;
for (b=0; b<SIZE; b++)
{
if ( MSB & k)
{
isLeadZero = false;
cout << 1;
}
else
{
if ( ! isLeadZero) cout << 0;
}k <<= 1; } cout << endl;
}
If you need to remove heading zeroes. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You should go in the opposite way, using multiplication (or bit shif, that is faster):
#include <iostream>
using namespace std;
void main()
{
unsigned int k = 731;
unsigned int b;
const int SIZE = sizeof(k) << 3;
const unsigned int MSB = 1 << (SIZE-1);
for (b=0; b<SIZE; b++)
{
cout << (( MSB & k) ? 1 : 0);
k <<= 1;
}
cout << endl;
}Or
#include <iostream>
using namespace std;
void main()
{
unsigned int k = 731;
unsigned int b;
const int SIZE = sizeof(k) << 3;
const unsigned int MSB = 1 << (SIZE-1);
bool isLeadZero= true;
for (b=0; b<SIZE; b++)
{
if ( MSB & k)
{
isLeadZero = false;
cout << 1;
}
else
{
if ( ! isLeadZero) cout << 0;
}k <<= 1; } cout << endl;
}
If you need to remove heading zeroes. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]i just tried your code and it works very well, there is one thing, i just cant seem to understand how it works line by line, since i want to learn c++ i think it wise to at least understand how it works, could you explain how your code outputs so many leading zero's, i find that very interesting.
-
i just tried your code and it works very well, there is one thing, i just cant seem to understand how it works line by line, since i want to learn c++ i think it wise to at least understand how it works, could you explain how your code outputs so many leading zero's, i find that very interesting.
In order to obtain the correct result (i.e. reversed bits) using your method, you need to multiply instead of dividing. At every iteration you have to:
- Test the most significative bit of the number.
- miltiply the number by two, so that the most significative bit is replaced by its adjacent one (the next you'll test).
In my code I replaced multiplication by
2
with left shift that is faster. Dealing with MSB (most significative bit) is a little trcky since its position depends on the data type, i.e. it is bit7
forunsigned char
s, bit15
for2
-byte data types (likeshort
on my machine) and bit31
for4
-byte data types (likeunsigned int
on my PC) hence the 'messy' lines in my routine, for instance:const int SIZE = sizeof(k) << 3;
Here SIZE is set to the length, expressed in bits, of
k
(sizeof
operator returns bytes, I multiply by8
using, again, left shift operator). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You should go in the opposite way, using multiplication (or bit shif, that is faster):
#include <iostream>
using namespace std;
void main()
{
unsigned int k = 731;
unsigned int b;
const int SIZE = sizeof(k) << 3;
const unsigned int MSB = 1 << (SIZE-1);
for (b=0; b<SIZE; b++)
{
cout << (( MSB & k) ? 1 : 0);
k <<= 1;
}
cout << endl;
}Or
#include <iostream>
using namespace std;
void main()
{
unsigned int k = 731;
unsigned int b;
const int SIZE = sizeof(k) << 3;
const unsigned int MSB = 1 << (SIZE-1);
bool isLeadZero= true;
for (b=0; b<SIZE; b++)
{
if ( MSB & k)
{
isLeadZero = false;
cout << 1;
}
else
{
if ( ! isLeadZero) cout << 0;
}k <<= 1; } cout << endl;
}
If you need to remove heading zeroes. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]thank you very much, i will play with this code and see what i can do with this new information. you said you multiplied by 8, but i see only 3 could you explain this, if it was a typo then never mind
-
thank you very much, i will play with this code and see what i can do with this new information. you said you multiplied by 8, but i see only 3 could you explain this, if it was a typo then never mind
You are welcome. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
You are welcome. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]i figured out what you meant by multiply by 8, it would be 2^3 which is 8 and what i seen in your code was 3. it seems this is a cool way to multiply and divide i hope this thread helps someone else learn something new as i did, thanks
-
hi everyone who reads this, i am in a digital electronics class and i decided to write a program that converts decimals to binary, i thought this was going to be simple and it was till i realized my my loop cout'ed the binary in reverse order. 731 in dec = 1011011011 and my program did it in reverse 1101101101 i used int's with modulus and division operators:
while (num)
{
rem=num%2;
num = num/2;
cout << rem;}
so i was wondering is there a simple way to reverse the output or could the division be done in reverse so the last output is first and the first last in my program? please note i am a beginner at c++ so i don't know how to do arrays as of yet and i am sure the output could be loaded into an array and printed in reverse order. thank you in advanced for any help.
There are many ways to do this. To use what you already have, add:
char s[128] = { '\0' };
int x = 0;while (num)
{
= (num & 1) ? '1' : '0';
num = num / 2; // num >>= 1
x++;
}cout << strrev(s) << endl;
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
There are many ways to do this. To use what you already have, add:
char s[128] = { '\0' };
int x = 0;while (num)
{
= (num & 1) ? '1' : '0';
num = num / 2; // num >>= 1
x++;
}cout << strrev(s) << endl;
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
its funny i was reading about arrays today and ways to reverse the output, had nothing yet, but i did learn how to make an array. what header files did you use? when i compile your modification to my code i get an unrecognizable output. thank you.
#include "stdafx.h"
#include using namespace std;int main()
{char s[128];
int x = 0;
int num =53while (num)
{
= (num & 1) ? '1' : '0';
num = num / 2; // num >>= 1
}cout << strrev(s) << endl;
system("pause");
return 0;
} -
its funny i was reading about arrays today and ways to reverse the output, had nothing yet, but i did learn how to make an array. what header files did you use? when i compile your modification to my code i get an unrecognizable output. thank you.
#include "stdafx.h"
#include using namespace std;int main()
{char s[128];
int x = 0;
int num =53while (num)
{
= (num & 1) ? '1' : '0';
num = num / 2; // num >>= 1
}cout << strrev(s) << endl;
system("pause");
return 0;
}Have you used the debugger to step through the code watching
s
andnum
?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
Have you used the debugger to step through the code watching
s
andnum
?"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
i haven't looked at the debugger messages, maybe i should, but as a beginner i may not even know what i am seeing so i will look into this. thanks