Mental decimal to binary conversion tips

her209

No Lifer
Oct 11, 2000
56,336
11
0
For converting subnet masks, its pretty easy:

I memorize: 128, 192, 224, 240, 248, 252, 254, 255. I know that 240 is in the middle, i.e. has 4 ones. I then add or remove ones to get to the desired number.

However, for converting a random number between 0 and 255, what mental tricks/tips do you use to quickly and mentally convert from decimal to binary and vice versa other than memorizing the 256 permutations?

EDIT: Quick meaning doing the conversion under 10 seconds. 5 seconds would be ideal. And 1-3 seconds being fast.
 
 

spidey07

No Lifer
Aug 4, 2000
65,469
5
76
I count on my fingers. Seriously. Meaning I go through this progression raising a finger from a closed fist each time. Basicially I start with MSB and work to LSB.

128, 192, 224, 240, 248, 252, 254
 

her209

No Lifer
Oct 11, 2000
56,336
11
0
Originally posted by: spidey07
I count on my fingers. Seriously.

128, 192, 224, 240, 248, 252, 254
But say you were given a non-subnet decimal number, e.g. 102. How would you quickly convert that to a binary?
 

spidey07

No Lifer
Aug 4, 2000
65,469
5
76
Originally posted by: her209
Originally posted by: spidey07
I count on my fingers. Seriously.

128, 192, 224, 240, 248, 252, 254
But say you were given a non-subnet decimal number, e.g. 102. How would you quickly convert that to a binary?

Memorization of base-2 and work down from MSB to LSB. Subnetting is actually easier than base-10 to base-2 conversion.

128 - NO
64 - YES
32 - YES (that gives 96, remainder 6)
16 - NO
8 - NO
4 - YES (that give 100, remainder 2)
2 - YES (that gives 102, remainder zero, all zeros moving to the right)
1 - NO

 

ScottMac

Moderator<br>Networking<br>Elite member
Mar 19, 2001
5,471
2
0
It always helped me to remember that I can count to 31 with one hand (five bits, and children always get a kick out of "four"), or 1023 with two.
For 1024 and above, I have to either take off my shoes or unzip my fly.
 

spidey07

No Lifer
Aug 4, 2000
65,469
5
76
Originally posted by: ScottMac
It always helped me to remember that I can count to 31 with one hand (five bits, and children always get a kick out of "four"), or 1023 with two.
For 1024 and above, I have to either take off my shoes or unzip my fly.

I don't know about you, but my penis is assuredly my most significant bit.

<geek humor>
 

her209

No Lifer
Oct 11, 2000
56,336
11
0
Originally posted by: spidey07
Originally posted by: her209
Originally posted by: spidey07
I count on my fingers. Seriously.

128, 192, 224, 240, 248, 252, 254
But say you were given a non-subnet decimal number, e.g. 102. How would you quickly convert that to a binary?
Memorization of base-2 and work down from MSB to LSB. Subnetting is actually easier than base-10 to base-2 conversion.

128 - NO
64 - YES
32 - YES (that gives 96, remainder 6)
16 - NO
8 - NO
4 - YES (that give 100, remainder 2)
2 - YES (that gives 102, remainder zero, all zeros moving to the right)
1 - NO
Well, I guess if you memorized the ranges for the 3 most significant bit, then you only have 5 bits to figure out. With the least significant bit being 0 or 1 if the number is even or odd.
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
Originally posted by: her209
Originally posted by: seemingly random
Seems like it would be easier to convert to hex.
Well, yeah.
I still find myself doing it sometimes unconsciously as the result of years of assembly language programming - even though I haven't done any for 10 years.
 

Pheran

Diamond Member
Apr 26, 2001
5,740
35
91
I guess everyone does this differently, but the way I work it mentally is to have my powers of 2 memorized (e.g. 2^4 = 16, 2^5 = 32 and so on). For masks, I just subtract the number from 256. So for 255.255.255.240, 256-240 = 16 = 2^4, so I have 4 bits worth of network and this is a /28. You also know that you have 16 available IPs that way (minus 2 for network/broadcast). It works exactly the same for 255.255.240.0, except you just have to add an extra 8 bits to the network and multiply your IPs by 256.

For converting arbitrary 0-255 values to binary, I just subtract the highest power that will come out of the number (starting with 128 and working down) and keep track of which bits are set. Also some numbers which are close to the main powers will be obvious (for example 129 = 10000001).
 

her209

No Lifer
Oct 11, 2000
56,336
11
0
I guess everyone does this differently, but the way I work it mentally is to have my powers of 2 memorized (e.g. 2^4 = 16, 2^5 = 32 and so on). For masks, I just subtract the number from 256. So for 255.255.255.240, 256-240 = 16 = 2^4, so I have 4 bits worth of network and this is a /28. You also know that you have 16 available IPs that way (minus 2 for network/broadcast). It works exactly the same for 255.255.240.0, except you just have to add an extra 8 bits to the network and multiply your IPs by 256.

For converting arbitrary 0-255 values to binary, I just subtract the highest power that will come out of the number (starting with 128 and working down) and keep track of which bits are set. Also some numbers which are close to the main powers will be obvious (for example 129 = 10000001).
For figuring out whether a.b.c.d is in the same subnet as w.x.y.z, I find that subtracting the last "weird" octet of the subnet mask from 256 work well. For example, given a subnet mask of 255.255.255.240, I take 256 -240 which gives 16. So every, the subnets go up every 16, i.e., if you were given an IP address of 192.168.1.1, you know that the current subnet is 192.168.1.0 and the next one is 192.168.1.16 and the next one after that is 192.168.1.32, etc.
 

her209

No Lifer
Oct 11, 2000
56,336
11
0
Sorry for bumping this old thread, but I came across a nice method of converting from binary-to-decimal in your head and vice-versa on the Internet that I thought I'd share:

For converting binary-to-decimal:

Start with a count of 0.
Work your way from the MSB to the LSB.
Double your count.
If the bit is 0, do nothing.
If the bit is 1, add one to your count.
Repeat until there are no more bits.

For example, given a binary number, e.g., 11001100:

bit - count=0
1 - 2*0+1=1
1 - 2*1+1=3
0 - 2*3=6
0 - 2*6=12
1 - 2*12+1=25
1 - 2*25+1=51
0 - 2*51=102
0 - 2*102=204

Therefore 11001100=204

Likewise, you can do the reverse to convert from decimal to binary:

Given 204
204 is divisible by 2, write 0 (LSB)
204/2=102 is divisible by 2, write 0
102/2=51 is not divisible by 2, write 1
(51-1)/2=25 is not divisible by 2, write 1
(25-1)/2=12 is divisible by 2, write 0
12/2=6 is divisible by 2, write 0
6/2=3 is not divisible by 2, write 1
(3-1)/2=1 is not divisible by 2, write 1 (MSB)