In programming, we often have to deal with numbers. Sometimes we would encounter situations where we are required to convert numbers from one numbering system to another numbering system. This tutorial concentrates on converting numbers between a binary numbering system to a decimal numbering system or vice-versa. This tutorial is to demonstrate the mathematical formula for converting between binary and decimal. The method of this article would represent and store binary numbers in strings format.
A decimal number is a base ten numbering system which uses ten symbols to represents values with the lowest value being a 0 and the highest value being a 9. A binary numbering system is a base two numbering system that only uses 1 and 0 to represent values.
How To Convert Binary To Decimal
The formula for converting a binary digit to a decimal digit is as follow (n*2p) + (n*2p) + …. . The breakdown of the formula can be explained in the following steps.
- Starting at the right of the binary string.
- Treat the binary digit zero as if it is a decimal zero, and one as if it is a decimal one.
- Each binary digit is multiplied to two by the power of their position from right to left of the string, and the first position being position zero.
- Sum the total of each digit multiply to two by the power of their position together. The result of this equation is the decimal value of the binary string.
An example of this formula is below.
Example: Converting binary 1111 to decimal Step 1: (1x2e3) + (1x2e2) + (1x2e1) + (1x2e0) Step 2: 8 + 4 + 2 1 Answer: 15
To put the above formula in a programming context, we would require a loop that will read the binary string from the end to the beginning of the string. When we start at the end of the string, our indexer for the loop would be the string’s length, and we are starting at the last position in the string. However, our power base for the multiplication procedure starts at zero. Therefore we would require another indexer to keep track of our power base, and every time we loop through our binary string, we would need to keep track and increase our power base by one. Another note is the power base of any number to zero will equates to multiply by one.
The for loop below demonstrate that we would use the times variable to keep track of our power base. At the end of each loop, we would multiply our count by two which in terms would equate to 2times. The most important requirement for this loop is the binary character at the current position have to multiply to the power base first in the loop before we can increase the power base for the next loop execution.
When binary digits are stored in string format, they have to be converted to number storage format for the computer system to recognize that it is a number. Without the conversion, the computer will treat the binary digit as a string format and will produce an error result or will throw an exception. Therefore the binary digits within our binary string must be converted to a number value before we can apply the mathematical equation on the digits.
for _i in (_invalue.length - 1).downto(0) _output = _output + (_invalue[_i].to_i * _times) _times = _times * 2 end
How To Convert Decimal To Binary
The formula for converting a decimal digit to a binary digit is as follow:
- Divide the decimal number by two until the decimal number become zero.
- Each time the decimal number is divide, a binary digit would be produced base on two conditions.
- If the decimal number is divisible by two, we produce a zero binary digit.
- If the decimal number is not divisible by two, we would minus one to the decimal number and divide the number by two. We then write down a one as the binary digit value.
- Write down the binary digit from each division and the first digit being the rightmost digit in the answer string.
Example: Convert 254 decimal to binary Equation Binary Digit Binary Digit After Each Equation Step 1 : 254 / 2 = 127 0 0 Step 2 : (127 - 1 ) / 2 = 63 1 1 0 Step 3 : (63 - 1 ) / 2 = 31 1 1 1 0 Step 4 : (31 - 1 ) / 2 = 15 1 1 1 1 0 Step 5 : (15 - 1 ) / 2 = 7 1 1 1 1 1 0 Step 6 : ( 7 - 1 ) / 2 = 3 1 1 1 1 1 1 0 Step 7 : ( 3 - 1 ) / 2 = 1 1 1 1 1 1 1 1 0 Step 8 : ( 1 - 1 ) / 2 = 0 1 1 1 1 1 1 1 1 0 Answer: 254 decimal value is 11111110 in binary value.
To put the above formula into a programming perspective, We can see the procedures are as follows. We need a loop that would execute while the input value is above zero. Every time the loop execute, we would have to keep track of two things. First, we check to see if the input value is divisible by two. If it is divisible by two we add zero as a binary digit to our output string. Second, if the input value is not divisible by two we add a one as a binary digit to our output string.
After any conditional clause executed, we would have to divide our input value by two bases on two conditions. If the number is not divisible by two, we minus one of the input value and divide it by two. If the number is divisible by two, we simply just divide the number by two. Any binary digits that we are adding to our output string must be to the left side of our output string.
This code block below demonstrates the above statements into a practical scripting format for Ruby. In the event where the input value is zero, we will return a four digit binary string of zero.
while (_invalue > 0) do if (_invalue % 2 == 0) _invalue = _invalue / 2 _output = "0" + _output elsif (_invalue % 2 != 0) _invalue = (_invalue - 1) / 2 _output = "1" + _output end end if (_invalue == 0) return "0000" end
This code block below is how you can compress all the if and else statement into two lines. The principle is still the same. Instead of evaluating the conditions in an if and else if conditional loop, we could evaluate the conditions through variable assignment base on conditions. The output variable will evaluate if the input value is not divisible by two then we assign a one to the output string. If it is divisible by two then we append zero to the front of our output string.
After assigning the output value in the loop, we then modify the input value base on conditions. Otherwise, the loop will execute indefinitely. If the current input value is divisive by two, we would divide the input value by two. Otherwise, we would assign the input value with the value of itself minus one then divide by two.
if (_invalue == 0) return "0000" end while (_invalue > 0) do _output = (_invalue % 2 != 0)? "1" + _output : "0" + _output _invalue = (_invalue % 2 == 0)? (_invalue / 2): (_invalue - 1) / 2 end
The source code below are the two functions that were mentioned in this article.
# "Copyright Notice", please do not remove. # Written by Kevin Ng # The full tutorial on this subject can be found @ http://kevinhng86.iblog.website or http://programming.world.edu. # This source code file is a part of Kevin Ng's Z library. # This source code is licenses under CCDL-1.0 A copy of CDDL1.0 can be found at https://opensource.org/licenses/CDDL-1.0 # End "Copyright Notice" # Note: Support the binary string as large as Ruby's mathematic equation allow. class LibZ def self.bindecZ (_invalue) _times = 1 _output = 0 for _i in (_invalue.length - 1).downto(0) _output = _output + (_invalue[_i].to_i * _times) _times = _times * 2 end return _output end def self.decbinZ(_invalue) _output = "" if (_invalue == 0) return "0000" end while (_invalue > 0) do _output = (_invalue % 2 != 0)? "1" + _output : "0" + _output _invalue = (_invalue % 2 == 0)? (_invalue / 2): (_invalue - 1) / 2 end # This code block is when the full set of 8 binary digit must be return. #while ( _output.length % 8 != 0) do # _output = "0" + _output #end return _output end end a = "11110111111111111111111111111111111111111111111111111" b = LibZ.bindecZ(a) c = LibZ.decbinZ(b) puts a puts b puts c
This post was written by Kevin and was first post @ http://kevinhng86.iblog.website.
Original Post Name: "Working With Number – Converting Binary And Decimal – Ruby".
Original Post Link: http://kevinhng86.iblog.website/2017/01/25/working-with-number-converting-binary-and-decimal-ruby/.