how to convert decimal number to binary | Newbie | | Join Date: Oct 2006
Posts: 1
| | |
a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen.
For example:
Enter a decimal number: 32
Its Binary Equivalent is : 000000100000
| | Member | | Join Date: Oct 2006
Posts: 96
| | | re: how to convert decimal number to binary Quote:
Originally Posted by saima a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen.
For example:
Enter a decimal number: 32
Its Binary Equivalent is : 000000100000
Do all process through recursive function....and print the values in the main function....
|  | Member | | Join Date: Oct 2006 Location: Phoenix, AZ
Posts: 40
| | | re: how to convert decimal number to binary
uhhhh stay away from recursive programming!!!!! yea its easy but it eats up mem and cpu usage!!!!!
i.e.
go to a porn site (and turn your popup blocker off!!!).
this is what recursive programing does if each window was a recursive function call.
pretty much each time you call a function (lets say b) the calling function (lets say a) is put on hold.
therefor function a calls b and both are open until b closes/ends so that function a may continue. with recursion a calls a so a is put on hold while another instance of a is running. do this enough times and your comp will crash (well might as well).
i'd suggest a loop for this. something along the lines of removed as per posting guidlines
the a%2 gives the remainder of number divided by 2.
num=b/2 decreases the number (moves over 1 bit) until num = 1 and the loop ends. this SHOULD give you a bin representation of the number. however its not going to be with 12 bits. try n figure out the rest if need furthur help post it up! gl
| | Newbie | | Join Date: May 2007
Posts: 3
| | | re: how to convert decimal number to binary removed as per posting guidlines
BOOYAH!!!!!111111 I AM TEH_MASTA1!11
|  | Expert | | Join Date: Feb 2007 Location: Halifax
Posts: 1,099
| | | re: how to convert decimal number to binary Quote:
Originally Posted by cplusplusmasta1337 removed as per posting guidlines
BOOYAH!!!!!111111 I AM TEH_MASTA1!11 Who apperently can't spell. ;) :D
Adrian
|  | Moderator | | Join Date: Oct 2006 Location: New York, United States of America
Posts: 3,428
| | | re: how to convert decimal number to binary Quote:
Originally Posted by vninja uhhhh stay away from recursive programming!!!!! yea its easy but it eats up mem and cpu usage!!!!! There are some problems that are just easier, conceptually and memory-wise, to solve with recursion. One example is traversing a binary tree - it's possible using loops, but I don't want to deal with all of that.
|  | Expert | | Join Date: Feb 2007 Location: Halifax
Posts: 1,099
| | | re: how to convert decimal number to binary Quote:
Originally Posted by Ganon11 There are some problems that are just easier, conceptually and memory-wise, to solve with recursion. One example is traversing a binary tree - it's possible using loops, but I don't want to deal with all of that. Further to what Ganon is saying, compiler optimisers nowadays can do some unrolling of recursion functions.
Adrian
| | Newbie | | Join Date: May 2007
Posts: 3
| | | re: how to convert decimal number to binary Again, code removed as per posting guidelines
BOOYA!!! 1337 PROGRAM RIGHT HERE. I spell fine.
Adrian.
|  | Expert | | Join Date: Feb 2007 Location: Halifax
Posts: 1,099
| | | re: how to convert decimal number to binary Quote:
Originally Posted by cplusplusmasta1337 Again, code removed as per posting guidelines
BOOYA!!! 1337 PROGRAM RIGHT HERE. I spell fine.
Adrian. ROFLMAO L7
Adrian
| | Newbie | | Join Date: May 2007
Posts: 3
| | | re: how to convert decimal number to binary removed as per posting guidelines
Soak it in boys. Lookin's for free, touchin's gonna cost ya.
Copy & Paste this for TEH_1337 HAX!!!!
Adrian. are you drunk?
// Nope but this oughta do it.
|  | Expert | | Join Date: Feb 2007 Location: Halifax
Posts: 1,099
| | | re: how to convert decimal number to binary Quote:
Originally Posted by cplusplusmasta1337 removed as per posting guidelines
Soak it in boys. Lookin's for free, touchin's gonna cost ya.
Copy & Paste this for TEH_1337 HAX!!!!
Adrian. are you drunk?
// Nope but this oughta do it. Whatever, you are banned for a week. Do it again and you will be banned permanently.
Adrian
| | Familiar Sight | | Join Date: Feb 2007 Location: Toronto Ontario
Posts: 211
| | | re: how to convert decimal number to binary
Sigh, stupid people....They'll never learn.
|  | Expert | | Join Date: Feb 2007
Posts: 1,739
| | | re: how to convert decimal number to binary Quote:
Originally Posted by Silent1Mezzo Sigh, stupid people....They'll never learn. Don't be rude and don't be a Savage
:)
Savage
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,270
| | | re: how to convert decimal number to binary
If the input number is always going to contain the same number of bits, then you could just do an & operation for each bit (or iterate through the bits) -
if (mydecimal&0x0004==0x0004)
-
{
-
//print a 1
-
}
-
else
-
{
-
//print a 0
-
}
-
-
if (mydecimal&0x0002==0x0002)
-
{
-
//print a 1
-
}
-
else
-
{
-
//print a 0
-
}
-
if (mydecimal&0x0001==0x0001)
-
{
-
//print a 1
-
}
-
else
-
{
-
//print a 0
-
}
-
|  | Expert | | Join Date: Feb 2007 Location: Halifax
Posts: 1,099
| | | re: how to convert decimal number to binary Quote:
Originally Posted by Plater If the input number is always going to contain the same number of bits, then you could just do an & operation for each bit (or iterate through the bits) -
if (mydecimal&0x0004==0x0004)
-
{
-
//print a 1
-
}
-
else
-
{
-
//print a 0
-
}
-
-
if (mydecimal&0x0002==0x0002)
-
{
-
//print a 1
-
}
-
else
-
{
-
//print a 0
-
}
-
if (mydecimal&0x0001==0x0001)
-
{
-
//print a 1
-
}
-
else
-
{
-
//print a 0
-
}
-
Two problems with this. 1. prints out the digits backwards. 2. used copy coding methods which are prone to error.
Other than that, it great. ;)
Adrian
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,270
| | | re: how to convert decimal number to binary
hehe actually if you look (and if I had added in a section for every bit) they would print out in the correct order.
And I never claimed to be good at c/c++, just saying it didn't look like anyone had given a solid answer to the post
| | Member | | Join Date: Apr 2007 Location: Paris
Posts: 42
| | | re: how to convert decimal number to binary
bitset<length> myBinary ((long) <your number>) ;
cout<<myBinary;
|  | Expert | | Join Date: Feb 2007 Location: Halifax
Posts: 1,099
| | | re: how to convert decimal number to binary Quote:
Originally Posted by fantasticamir bitset<length> myBinary ((long) <your number>) ;
cout<<myBinary; Hey, didn't know that one. Learn something every day. ;)
Adrian
|  | Expert | | Join Date: Feb 2007
Posts: 1,739
| | | re: how to convert decimal number to binary Quote:
Originally Posted by AdrianH Hey, didn't know that one. Learn something every day. ;)
Adrian I suppose that we exist to learn.
;)
Savage
| | Newbie | | Join Date: Jul 2007
Posts: 2
| | | re: how to convert decimal number to binary Quote:
Originally Posted by fantasticamir bitset<length> myBinary ((long) <your number>) ;
cout<<myBinary; GREAT: Two questions.
code for binary/hexadecimal/decimal <-->binary/hexadecimal/decimal.
| | Newbie | | Join Date: Jul 2007
Posts: 2
| | | re: how to convert decimal number to binary Quote:
Originally Posted by fantasticamir bitset<length> myBinary ((long) <your number>) ;
cout<<myBinary; GREAT: Two questions.
First, the code for binary/hexadecimal/decimal <-->binary/hexadecimal/decimal.
Second, is there a reference text/site which has indexed code snippets for math/engineering C++.
TIA
WestCoast101
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,270
| | | re: how to convert decimal number to binary Quote:
Originally Posted by WestCoast101 GREAT: Two questions.
First, the code for binary/hexadecimal/decimal <-->binary/hexadecimal/decimal.
Second, is there a reference text/site which has indexed code snippets for math/engineering C++.
TIA
WestCoast101 The binary conversions should be above.
Hex is simple.
.ToString("X") on any integer will give you it's value as a hex string.
Int.Parse(string); is overloaded with a 2nd value stateing if it should accept a hex string or not.
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,553
| | | re: how to convert decimal number to binary Quote:
Originally Posted by Plater The binary conversions should be above.
Hex is simple.
.ToString("X") on any integer will give you it's value as a hex string.
Int.Parse(string); is overloaded with a 2nd value stateing if it should accept a hex string or not. Too bad this isn't C++.
|  | Moderator | | Join Date: Apr 2007 Location: New England
Posts: 7,270
| | | re: how to convert decimal number to binary Quote:
Originally Posted by weaknessforcats Too bad this isn't C++. Ooops, didn't notice what forum I was in. (got this outa control pannel)
I am "borrowing" this code from an embeded development helpfile and modifying it. My C is a little rusty -
BYTE FromHexHelper(char digit)
-
{
-
if(digit<='9')
-
return(digit-'0');
-
else
-
return((toupper(digit)-'A')+10);
-
}
-
-
BYTE FromHex(char[2] hexdigit)
-
{
-
int lo,hi;
-
-
hi = FromHexHelper(hexdigit[0]);
-
lo = FromHexHelper(hexdigit[1]);
-
if(lo==0xdd)
-
return(hi);
-
else
-
return( hi*16+lo );
-
}
-
-
//pass in a char[2] (or bigger) array to hexdigit
-
void ToHex(byte val, char *hexdigit)
-
{
-
byte hi=0x00;
-
byte lo=0x00;
-
-
hi=0xF0&val;
-
lo=0x0F&val;
-
//you could do a switch statement on hi and lo to determine 0-9A-F
-
//then populate hexdigit[0] and hexdigit[1]
-
}
-
| | Newbie | | Join Date: Jul 2007 Location: bangalore
Posts: 2
| | | re: how to convert decimal number to binary Quote:
Originally Posted by saima a program that takes a decimal number input from the user and displays its binary number equivalent in 12 bits on the screen.
For example:
Enter a decimal number: 32
Its Binary Equivalent is : 000000100000 hi,
i am new to the forum can you suggest me how to send my queries and how to view the replies to my queries?
|  | Expert | | Join Date: Dec 2006
Posts: 783
| | | re: how to convert decimal number to binary Quote:
Originally Posted by marulasiddesh hi,
i am new to the forum can you suggest me how to send my queries and how to view the replies to my queries? Hi,
You kindly read Posting Guidelines
Regards
| | Newbie | | Join Date: Dec 2008
Posts: 1
| | | re: how to convert decimal number to binary
< spoonfeeding code deleted >
| | Expert | | Join Date: Mar 2008 Location: Naperville, Illinois U.S.
Posts: 903
| | | re: how to convert decimal number to binary
You could repeatedly extract one nibble at a time from your value, using that nibble to index into an array of strings. The binary-string array contains 4-character strings such as "0000", "0001", etc. The hex-string array contains 1-character strings such as "0", "1", etc. Each such array has 16 entries.
How many nibbles are in the value? You either have to have secret knowledge of your compiler that you hard-code into your software or perhaps you could look at <limits.h> to see if your program can adapt itself to the compiler.
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,553
| | | re: how to convert decimal number to binary
No one has mentioned an algorithm to solve this.
To check that a bit is set in a binary column, all you do is:
(32/32) = 1 and 1%2 is 1 the 32-bit is set
(32/16) = 2 and 2 % 2 is 0 the 16-bit is not set
etc...
|  | Member | | Join Date: Jul 2008
Posts: 100
| | | re: how to convert decimal number to binary Quote:
Originally Posted by weaknessforcats No one has mentioned an algorithm to solve this.
To check that a bit is set in a binary column, all you do is:
(32/32) = 1 and 1%2 is 1 the 32-bit is set
(32/16) = 2 and 2 % 2 is 0 the 16-bit is not set
etc... If you're using STL bitsets you can do myBitset.test(position);
| | Member | | Join Date: Nov 2008
Posts: 64
| | | re: how to convert decimal number to binary
I prefer looping over the number of bits and doing a check along the lines of -
if ( num & (1 << curbit) )
-
If that is true, then bit curbit is set (the least significant bit being bit 0). This is very similar to what weaknessforcats said, except that one is -
if ( (num >> curbit) & 1 )
-
:D
|  | Expert | | Join Date: Aug 2007 Location: Belgium
Posts: 1,150
| | | re: how to convert decimal number to binary
I believe itoa() with base 2 would be kinda spoiling the fun.
That, and you don't learn anything from it.
| | Newbie | | Join Date: Jan 2010
Posts: 1
| | | re: how to convert decimal number to binary
if you guys need an online tool to convert a decimal value to binary, try this decimal to binary tool
David
|  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 229,136 network members.
|