Understanding Binary

Adam Gibson
Adam Gibson
Published in
4 min readJun 20, 2018
Binary to decimal conversion

Binary is a base 2 numbering system which means there are only two number options to choose from: 0 and 1. This is in contrast to decimal which has ten options: 0,1,2,3,4,5,6,7,8,9. Or hexadecimal which has sixteen options: 0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f. In computer science, one binary digit is called a “bit”. Eight binary digits is a called a “byte”.

Binary may seem strange, but it functions basically the same as decimal except there are only two numerical symbols to choose from instead of ten.

In decimal, each place is a power of ten since there are ten potential symbols that could be used in each place:

In this example, the number 1,765,321 is shown. Each place value is essentially reverse indexed by the power that ten is raised to. So the 1s place is indexed at power 0, 10s at power 1, 100s at power 2 and so on.

In binary, each place number is a power of two since there are only two potential symbols:

The same indexing is happening for each place value, but now its all powers of two: 1s, 2s, 4s, 8s, 16s, 32s and so on.

The 0s and 1s in each place keeping track of how many of a particular place value there are. In this example, there is one 128s, 16s,8s, and 2s but zero 64s, 32s, 4s, and 1s.

Counting in Binary

Like in decimal, binary counting works by incrementing each symbol in each place. When the last available symbol for for that numbering system is reached, it goes back to zero and the next place value is incremented by one and so on.

Here is an example of counting to ten in binary:

Conversion process from binary to decimal

Using the example for before, you can see that finding the decimal value simply requires multiplying the place value(128, 64, 32 etc) by either 1 or 0 and then adding up all of the products.

Conversion process of decimal to binary

Converting from decimal to binary requires repeatedly dividing by two and in each step the quotient becomes the new dividend. The remainders are tracked and at the end, they are reversed to find the binary number.

In this example, the binary form of twenty is 10100

Implementations in Java

The first method, BinaryToDecimal, is an implementation of the binary-to-decimal conversion process. It takes a string as an argument and returns an integer. The algorithm iterates over the binary string, converting each character to its integer equivalent. That number is then multiplied by two raised to the appropriate power of the current place value.The power is found by subtracting one and the current string index from the length of the string. The total is added to the dec variable. After the loop terminates, dec is returned for the answer.

public static int BinaryToDecimal(String bin) {
int dec = 0;
for(int i=0;i<bin.length();i++) {
int y = bin.charAt(i) - '0';
dec += Math.pow(2, bin.length()-1-i) * y;
}
return dec;
}

The next method, DecimalToBinary, is an implementation of the decimal-to-binary conversion process. The method takes an integer argument and returns a string. The method iteratively divides the provided decimal by two and on each pass converts the remainder to a string and adds it to the bin variable. A reversed copy of the bin string is made called binCopy which is returned as the binary result.

public static String DecimalToBinary(int dec) {
String bin = "";
while (dec > 0){
bin += Integer.toString(dec%2);
dec = dec/2;
}
String binCopy = ""; for(int i=1; i <= bin.length(); i++) {
binCopy += bin.charAt(bin.length()-i);
}
return binCopy;
}

--

--