Formula: Decimal To Binary
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
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
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
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
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement