d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Decimal To Binary
Add Reply New Topic New Poll
Member
Posts: 35,771
Joined: Feb 2 2006
Gold: 255.05
Nov 26 2021 03:48am
Hey!

I recently started a programming course and we have begun looking into binary numbers, how they are converted from decimals and back etc.

I have a hard time understanding why 1024 equals 1000 0000 0000 and not 0100 0000 0000 as 1024 fits in the 11th column.
I know there are two methods "Descending Powers of Two and Subtraction" & " Short Division by Two with Remainder." But I still can'tt understand why its 12th and not 11th column.

This is what I can find busing google "Increase the exponent by 1 until you reach the number close to the decimal you are starting with". But 1024 fits so why use 2048 and divide by 2?

Appreciate your help and sorry for the bad explanation ^_^
Member
Posts: 35,771
Joined: Feb 2 2006
Gold: 255.05
Nov 26 2021 04:45am
Solved it. I added a 0 too much <_<
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
Nov 26 2021 06:20am
log_2 (1024) = 10
1 00 0000 0000

using log base 2 can make it faster
Member
Posts: 2,978
Joined: Jul 5 2005
Gold: 8,211.00
Feb 19 2022 04:58pm
Binary is just the same as decimal, but instead of every position being 10 to a power (ones place = 10^0, tens place = 10^1, hundreds place = 10^2, etc.), it is 2 to a power (one = 2^0, two = 2^1, four = 2^2, etc.)

And with binary, all you can get is the power of two because the only number you can stick in that place is a 1. You can't multiply by a number like in decimal for that position. For instance the number 35 in decimal is 5*10^0 + 3*10^1.

If you want to find 1024, that is the same as 2^ 10. Since we start the farthest position as 2^0, there will be a total of 11 digits.

100 0000 0000 = 1*2^ 10 + 0*2^9 + 0*2^8 + 0*2^7+ 0*2^6 + 0*2^5 + 0*2^4 + 0*2^3 + 0*2^2 + 0*2^1 + 0*2^0 = 1024

Hopefully you passed your programming course!

This post was edited by Pelnied on Feb 19 2022 05:00pm
Member
Posts: 14,910
Joined: Oct 21 2011
Gold: 38,363.00
Mar 26 2022 04:29am
Quote (moutonguerrier @ Nov 26 2021 01:20pm)
log_2 (1024) = 10
1 00 0000 0000

using log base 2 can make it faster


This.

Just remember that changing from a base A to a base B can be done using iterations of INT(log_A(x)) and exp_B(x) because a sum of n numbers in base A is EQUAL to the sum of the same numbers in base B

For example : transform 1030_10 in a number base 2 (X_2).
Step 1 ) INT(log2(1030)) = INT(10,something) = 10, exp_10(10) = 10 000 000 000, we have to transform the remaining part that is 1030 - exp_10(2) = 1030 - 1024 = 6
Step 2 ) INT(log2(6)) = INT(2,something) = 2, exp_10(2) = 100, remaining part is 6 - exp_2(2) = 6-4 = 2
Step 3 ) INT(log2(2)) = INT(1) = 1 (we will stop at step 3 because no more decimals to be transformed), exp_10(1) = 10

Summing steps will give that 1030_10 = 1024_10 + 4_10 + 2_10 = 10000000000_2 + 100_2 + 10_2 = 1 000 000 110

This is not the formal demonstration of course, the mathemathics behind it starts from the explanation given by @pelnied , I provided a different PoV because this is a programmer forum and the one I explained is pretty much one of the common algorithms to solve these problems.

This post was edited by NomadJe on Mar 26 2022 04:32am
Member
Posts: 4,353
Joined: May 12 2009
Gold: 6,510.00
Apr 15 2022 06:20pm
Quote (moutonguerrier @ Nov 26 2021 08:20am)
log_2 (1024) = 10
1 00 0000 0000

using log base 2 can make it faster


floor(log_2(n)) + 1 gives you the "position" of the highest 1 in binary of form for number n. If n is a power of 2, only one 1 is needed, thus 1024 becomes 1 + 10 zeros
Member
Posts: 3,372
Joined: Sep 2 2006
Gold: 22,951.70
May 15 2022 10:45pm


Quote (Disturbedxx @ Nov 26 2021 05:48am)
Hey!
I have a hard time understanding why 1024 equals 1000 0000 0000 and not 0100 0000 0000 as 1024 fits in the 11th column.


1024 does NOT equal 1000 0000 0000 in binary. IT IS 0100 0000 0000

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll