Connecting Tech Pros Worldwide Forums | Help | Site Map

how to convert decimal number to binary

Newbie
 
Join Date: Oct 2006
Posts: 1
#1: Oct 30 '06
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
#2: Oct 30 '06

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....
vninja's Avatar
Member
 
Join Date: Oct 2006
Location: Phoenix, AZ
Posts: 40
#3: Oct 30 '06

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
#4: May 17 '07

re: how to convert decimal number to binary


removed as per posting guidlines


BOOYAH!!!!!111111 I AM TEH_MASTA1!11
AdrianH's Avatar
Expert
 
Join Date: Feb 2007
Location: Halifax
Posts: 1,099
#5: May 17 '07

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
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#6: May 17 '07

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.
AdrianH's Avatar
Expert
 
Join Date: Feb 2007
Location: Halifax
Posts: 1,099
#7: May 17 '07

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
#8: May 18 '07

re: how to convert decimal number to binary


Again, code removed as per posting guidelines


BOOYA!!! 1337 PROGRAM RIGHT HERE. I spell fine.

Adrian.
AdrianH's Avatar
Expert
 
Join Date: Feb 2007
Location: Halifax
Posts: 1,099
#9: May 18 '07

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
#10: May 22 '07

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.
AdrianH's Avatar
Expert
 
Join Date: Feb 2007
Location: Halifax
Posts: 1,099
#11: May 22 '07

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
#12: May 22 '07

re: how to convert decimal number to binary


Sigh, stupid people....They'll never learn.
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,739
#13: May 22 '07

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
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,270
#14: May 22 '07

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)
Expand|Select|Wrap|Line Numbers
  1. if (mydecimal&0x0004==0x0004)
  2. {
  3. //print a 1
  4. }
  5. else
  6. {
  7. //print a 0
  8. }
  9.  
  10. if (mydecimal&0x0002==0x0002)
  11. {
  12. //print a 1
  13. }
  14. else
  15. {
  16. //print a 0
  17. }
  18. if (mydecimal&0x0001==0x0001)
  19. {
  20. //print a 1
  21. }
  22. else
  23. {
  24. //print a 0
  25. }
  26.  
AdrianH's Avatar
Expert
 
Join Date: Feb 2007
Location: Halifax
Posts: 1,099
#15: May 23 '07

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)

Expand|Select|Wrap|Line Numbers
  1. if (mydecimal&0x0004==0x0004)
  2. {
  3. //print a 1
  4. }
  5. else
  6. {
  7. //print a 0
  8. }
  9.  
  10. if (mydecimal&0x0002==0x0002)
  11. {
  12. //print a 1
  13. }
  14. else
  15. {
  16. //print a 0
  17. }
  18. if (mydecimal&0x0001==0x0001)
  19. {
  20. //print a 1
  21. }
  22. else
  23. {
  24. //print a 0
  25. }
  26.  

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
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,270
#16: May 23 '07

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
#17: May 23 '07

re: how to convert decimal number to binary


bitset<length> myBinary ((long) <your number>) ;

cout<<myBinary;
AdrianH's Avatar
Expert
 
Join Date: Feb 2007
Location: Halifax
Posts: 1,099
#18: May 23 '07

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
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,739
#19: May 23 '07

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
#20: Jul 11 '07

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
#21: Jul 11 '07

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
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,270
#22: Jul 12 '07

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
#23: Jul 12 '07

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++.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,270
#24: Jul 12 '07

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
Expand|Select|Wrap|Line Numbers
  1. BYTE FromHexHelper(char digit) 
  2. {
  3.    if(digit<='9')
  4.      return(digit-'0');
  5.    else
  6.      return((toupper(digit)-'A')+10);
  7. }
  8.  
  9. BYTE FromHex(char[2] hexdigit) 
  10. {
  11.    int lo,hi;
  12.  
  13.    hi = FromHexHelper(hexdigit[0]);
  14.    lo = FromHexHelper(hexdigit[1]);
  15.    if(lo==0xdd)
  16.      return(hi);
  17.    else
  18.      return( hi*16+lo );
  19. }
  20.  
  21. //pass in a char[2] (or bigger) array to hexdigit
  22. void ToHex(byte val, char *hexdigit)
  23. {
  24.    byte hi=0x00;
  25.    byte lo=0x00;
  26.  
  27.    hi=0xF0&val;
  28.    lo=0x0F&val;
  29.    //you could do a switch statement on hi and lo to determine 0-9A-F
  30.    //then populate hexdigit[0] and hexdigit[1]
  31. }
  32.  
Newbie
 
Join Date: Jul 2007
Location: bangalore
Posts: 2
#25: Jul 20 '07

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?
zodilla58's Avatar
Expert
 
Join Date: Dec 2006
Posts: 783
#26: Jul 20 '07

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
#27: Dec 2 '08

re: how to convert decimal number to binary


< spoonfeeding code deleted >
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 903
#28: Dec 2 '08

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
#29: Dec 3 '08

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...
MrPickle's Avatar
Member
 
Join Date: Jul 2008
Posts: 100
#30: Dec 3 '08

re: how to convert decimal number to binary


Quote:

Originally Posted by weaknessforcats View Post

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
#31: Dec 3 '08

re: how to convert decimal number to binary


I prefer looping over the number of bits and doing a check along the lines of
Expand|Select|Wrap|Line Numbers
  1. if ( num & (1 << curbit) )
  2.  
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

Expand|Select|Wrap|Line Numbers
  1. if ( (num >> curbit) & 1 )
  2.  
:D
YarrOfDoom's Avatar
Expert
 
Join Date: Aug 2007
Location: Belgium
Posts: 1,150
#32: Dec 3 '08

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
#33: 2 Weeks Ago

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
Reply