Binary numbers (CSCI 2824, Spring 2015)In this lecture, we cover the following topics:
The Amazing Binary WorldPlace Valued Number SystemsA decimal number n=1092991367 is quite familiar and seems quite natural to us. This is most probably because ancient humans used the fingers in their hands to count and convey numbers to each other. For example, we have learnt in high school that the number ’'n’’ above is odd (not divisible by 2), leaves a remainder of 2 when divided by 3, is not divisible by 5 and so on. On the other hand, what can we say about the number ’’1001001’’? Is it odd or even? Is it divisible by 3? How about 5? Binary numbers are not easy to handle mentally, even for experienced computer scientists, unless one has worked with them for a long time. With the advent of digital computers, it became much more easier to deal with bits inside a computer that could either be ’'on’’ or ’'off’’ conveying a 1 or a 0. Therefore, the use of binary numbers is quite prevalent in computer arithmetic. It is hard to imagine doing things any other way. As the joke goes: there are 10 people on earth, those who understand binary and those who do not. Binary NumbersLet us first consider whole numbers and ignore fractions. A binary number is a string of 0s and 1s. Generally, we remove the leading zeros and simply write starting from the first 1 bit. In other words the number 0000111 is the same as 111. Examples of binary numbers:
The leftmost bit of a number is called its most significant bit. The rightmost bit is called the least significant bit. Can you guess why these bits are called so? We can convert binary numbers to decimals rather easily. Take the number . Its value in decimal can be found by the following procedure: We sometimes write the base of the number as a subscript to resolve ambiguities. E.g., is the number one hundred and eleven to the base 10, which is entirely different from , which is in fact . Here are some decimal numbers corresponding to binary numbers:
Conversion between Binary Numbers and Decimal NumbersTo convert a decimal number to binary involves the following procedure that outputs starting from the least significant bit to the most.
Here is some C code: Code to Convert a Decimal Integer to Binary
void convertToBinary(int n){ int result[64]; int nBits = 0; if ( n < 0) { /* Input is negative */ printf("- "); n = -n; } if (n == 0){ printf ("0"); return; } while ( n > 0){ /* Check <span class="statement">if</span> n is even or odd. You can instead write result[nBits] = n%2; */ if (n % 2 == 0){ result[nBits] = 0; } else { result[nBits] =1; } nBits ++; n = n/2; /* Divide by 2 */ } /* Print starting from the end */ for (i = nBits -1; i >= 0; i --){ printf ("%d", result[i]); } printf ("\n"); } E.g., Consider the number = 161 in decimal. Here are the steps:
Therefore . Some Binary TricksNow let us try to answer the following questions:
Checking Binary Divisibility by 3Take any number in binary say . To test if it is divisible by three,
Some examples are worked out below:
We will show why this trick works when we do modular arithmetic. Then we will also be able to tackle divisibility by 5, 7 and 11 as well :-) Representing numbers inside a computerBinary fractionsJust as we are able to represent fractions in decimals, we are also able to represent binary fractions. A binary fraction is written as . We can of course have whole numbers plus fractions just like we do for decimal numbers. The point used here will be called the radix point or sometimes confusingly people call it the decimal point. Again, the first bit after radix point represents , the second bit represents , the third bit represents and so on. Let us again give some examples:
Now, a natural question is how do we represent fractions like or in binary. Turns out that we cannot quite represent in finitely many bits. In fact we have . Note that it has a followed by an infinitely repeating pattern of at the end. Likewise is an infinitely repeating sequence of after the radix point. We have seen this situation in decimal as well. For example is an infinitely repeating sequence of s. What if we truncate the binary expansion of after say bits? We then have a number such as (not the exact value) that is close to but not quite . In fact, the standard float used in most programming language is able to represent up to bits of a binary fraction. The rest of it is simply truncated off. Arithmetic Error in ComputersDoes it really matter that a 0.1 inside a computer is really ? In some cases it does. Sigfried Rump pointed out the following dramatic example: Here is a C program to compute : C program for Rump's example
#include <stdio.h> float a = 77617; float b = 33096; int main(){ float f = 333.75 * b*b*b*b*b*b + a*a * ( 11 * a*a * b*b - b*b*b*b*b*b - 121 * b*b*b*b -2) + 5.5 * b*b*b*b*b*b*b*b + a/(2 * b) ; printf("f = %f \n", f); return 1; } When run on a 64 bit intel we get f = -44450695952321879337122922496.000000. That seems pretty good. But in reality, the answer should be . In other words, the answer computed on my desktop is off by a factor of !!! Floating point errors due to finite binary arithmetic is a fact in life and arises because fractions can have infinitely long binary expansions and computers can only represent a finite (small) part of this. |