Discrete Structures (CSCI 2824, Spring 2014)The Amazing Binary WorldIn this lecture, we will consider some basics of the binary number system and representing numbers inside a computer. Place 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 divsible 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 NumberLet 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. We sometimes write the base of the number as a subscript to resolve ambiguities.
Eg.,
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 Here are some decimal numbers corresponding to binary numbers:
Converting Decimal to BinaryTo 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"); } Eg., Consider the number n = 161 in decimal. Here are the steps:
Therefore Some Binary TricksNow let us try to answer the following questions:
Binary Divisibility by 3 CheckTake any number in binary say
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 :-) Binary fractionsJust as we are able to represent fractions in decimals, we are also able to represent binary fractions. A binary fraction is written as
Again, the first bit after radix point represents Let us again give some examples:
Now, a natural question is how do we represent fractions like Likewise We have seen this situation in decimal as well. For example What if we truncate the binary expansion of Arithmetic Error in ComputersDoes it really matter that a 0.1 inside a computer is really 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 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. |