Formula: Decimal To Binary

Started by
4 comments, last by mdias 21 years, 4 months ago
Hi, Does anyone knows where canI get the formula to convert a decimal value to binary ?? I''ve been searching on the net but haven''t found anything yet. Kamikaze
Advertisement
So you have an int, and you want to convert it to a string of ''1'' and ''0'' characters?

It''s not so hard, you can try using the & operator to determine if a particular bit is set, and testing each bit for a 32 bit number. Start at 0x80000000 (which is 1 << 31) and keep shifting right until you get to 1.

Helpful links:
How To Ask Questions The Smart Way | Google can help with your question | Search MSDN for help with standard C or Windows functions
Displaying an integer in binary form in C/C++/Java is as simple as setting a flag.
Hi

if you want something more general to convert between different number-bases you can also look at my code here


Runicsoft -- home of my open source Function Parser and more
This is where the mod % operator comes in handy.
look:
say we want 32d to binary
32 / 2 = 16 R0
16 / 2 = 8 R0
8 / 2 = 4 R0
4 / 2 = 2 R0
2 / 2 = 1 R0
1 / 2 = 0 R1
the remainders in reverse order is 100000
so if you use the mod operator you can get the 1''s and 0''s
32 % 2 = 0
16 % 2 = 0
8 % 2 = 0
4 % 2 = 0
2 % 2 = 0
1 % 2 = 1
100000

Hope this helps.
Thanx guys, I made it

Kamikaze

This topic is closed to new replies.

Advertisement