Professional Documents
Culture Documents
Digital circuits are inherently binary in nature, but several types of representations of numerical data are in use. The representation of an unsigned integer can be done in binary, octal, decimal or hexadecimal. For display purposes, each decimal digit is often represented by a four-bit binary number in a system called binary coded decimal (BCD . Conversions bet!een these representations can be handled in a routine manner. The representation of signed numbers presents more problems and those problems are addressed in various !ays. "ome of the codes used are #sign magnitude#, #offset binary#, #$%s complement#, #excess-&#, #'$$(#, and #)ray#. * table can sho! the display of four-bit integers.
Ones' complement The ones' complement of a binary number is defined as the value obtained by inverting all the bits in the binary representation of the number (swapping 0's for 1's and vice-versa). The ones' complement of the number then behaves li e the negative of the original number in most arithmetic operations. !owever" unli e two's complement" these numbers could not have widespread use because of issues li e negative #ero" end-around borrow" etc. Two's complement The two's complement of a binary number is defined as the value obtained by subtracting the number from a large power of two (specifically" from $% for an %-bit two's complement). The two's complement of the number then behaves li e the negative of the original number in most arithmetic" and it can coe&ist with positive numbers in a natural way. ' two's-complement system" or two's-complement arithmetic" is a system in which negative numbers are represented by the two's complement of the absolute value()1* this system is the most common method of representing signed integers on computers.)$* +n such a system" a number is negated (converted from positive to negative or vice versa) by computing its ones' complement (i.e. its bitwise negation) and adding one. 'n %-bit two's-complement numeral system can represent every integer in the range ,$%,1 to $%,1,1 while ones' complement can only represent integers in the range ,($%,1,1) to $%,1,1 -hy it wor s .iven a set of all possible %-bit values" we can assign the lower (by binary value) half to be the integers from 0 to ($%,1,1) inclusive and the upper half to be ,$%,1 to ,1 inclusive. The upper half can be used to represent negative integers from ,$%,1 to ,1 because" under addition modulo $% they behave the same way as those negative integers. That is to say that because i / 0 mod $% 1 i / (0 , $%) mod $% any value in the set 20 / $% 3 is an integer4 can be used in place of 0.
5or e&le" with eight bits" the unsigned bytes are 0 to $66. 7ubtracting $68 from the top half (1$9 to $66) yields the signed bytes ,1$9 to 1$:. The relationship to two's complement is realised by noting that $68 1 $66 / 1" and ($66 , &) is the ones' complement of &. ;ecimal Two's complement 1$: 0111 1111 8< 0100 0000 1 0000 0001 0 0000 0000 ,1 1111 1111 ,8< 1100 0000 ,1$: 1000 0001 ,1$9 1000 0000 7ome special numbers to note =&le ,>6 modulo $68 is e?uivalent to 181 since ,>6 / $68 1 ,>6 / $66 / 1 1 $66 , >6 / 1 1 180 / 1 1 181 1111 1111 $66 , 0101 1111 , >6 11111111111 11111 1010 0000 (ones' complement) 180 / 1 / 1 11111111111 11111 1010 0001 (two's complement) 181
Excess-3 Code
,n this code, a digit is represented by adding & to the number and then converting it to a '-bit binary number. ,t can be used for the representation of multi-digit decimal numbers as can BCD. This code along !ith the '$$( code have some advantages for performing decimal arithmetic.
Gray Code
The reflected binary code, also known as Gray code after Frank Gray, is a binary numeral system where two successive values differ in only one bit. It is a non-weighted code
* code designed to minimi/e errors during the transition bet!een one code value and another, this code changes only one bit from one se.uential value to another. ,t is used for shaft encoders and other applications !here mechanical control using a digital code re.uires the minimi/ing of stresses !hich might occur if an erronius value !as sent during a transition. The rule for generating the code is0 begin !ith all /eros and change the least significant bit that !ill bring you to a ne! state.
Dec 0 1 2 3 4 5 6 7
4221 Code
There are several codes !hich use binary numbers to code for single digits in multi-digit decimal numbers. This code assigns the values ',$,$,( instead of -,',$,( to the four binary digits. ,t can properly be called binary coded decimal, but that designation (BCD is usually reserved for the -,',$,( code. This code is sometimes !ritten '$$( BCD. This code along !ith the excess-& code have some advantages for performing decimal arithmetic.
(88 (88( (88 (8(8 (88 (8(( (88 ((88 (88 ((8( (88 (((8 (88 (((( (8( 8888 (8( 888( (8( 88(8 (8( 88(( (8( 8(88 (8( 8(8( (8( 8((8 (8( 8((( (8( (888 (8( (88( (8( (8(8
'; '* 'B 'C 'D '2 'F :8 :( :$ :& :' :: :9 :4 ::; :*
i = 1 l m n o p . r s t u v ! x y /
((8 (88( ((8 (8(8 ((8 (8(( ((8 ((88 ((8 ((8( ((8 (((8 ((8 (((( ((( 8888 ((( 888( ((( 88(8 ((( 88(( ((( 8(88 ((( 8(8( ((( 8((8 ((( 8((( ((( (888 ((( (88( ((( (8(8
; blan1 . ( A B D G , I
8(( (888 8(( (88( 8(8 8888 8(8 (((8 8(8 (888 8(8 (8(( 8(8 8(88 8(8 (8(8 8(8 (88( 8(8 ((8( 8(8 (((( 8(8 ((88 8(( ((8(
F2THF@ 888 ((8( 3@F22D 888 (8(8 8 8 8 8(( 8888 8(( 8888 8(( 8888
EBCD"C Code
Char 2BCD,C * B C D 2 F ) 627 Char 2BCD,C C E F " T H J 627 Char 2BCD,C ' : 9 4 ; 627 ((88 888( C( ((88 88(8 C$ ((88 88(( C& ((88 8(88 C' ((88 8(8( C: ((88 8((8 C9 ((88 8((( C4 ((8( 8((( D4 ((8( (888 D((8( (88( D; (((8 88(8 2$ (((8 88(( 2& (((8 8(88 2' (((8 8(8( 2: (((( 8(88 F' (((( 8(8( F: (((( 8((8 F9 (((( 8((( F4 (((( (888 F(((( (88( F; ...
blan1 ...
6 , < > 3 ? @ +
((88 (888 C((88 (88( C; ((8( 888( D( ((8( 88(8 D$ ((8( 88(( D& ((8( 8(88 D' ((8( 8(8( D: ((8( 8((8 D9
K 7 L M 8 ( $ &
(((8 8((8 29 (((8 8((( 24 (((8 (888 2(((8 (88( 2; (((( 8888 F8 (((( 888( F( (((( 88(8 F$ (((( 88(( F&
. ( A B D G
#arity C ec$s
2rrors in digital code !ill result in the changing of a 8 to a ( or vice versa. +ne helpful method for determining if a single error of that type has ocurred is to chec1 the evenness or oddness of the sum of the set bits. To facilitate this chec1 an extra bit called the parity bit is added to each !ord in a data transmission. ,n the even-parity method the parity bit is chosen so that the total number of (%s including the parity bit is even. The receiver chec1s the parity to detect any singlebit errors. The same thing can be accomplished !ith an odd-parity method, so it is necessary to 1no! !hich is being used in order to communicate !ith a host computer. ,t !ill also be necessary to 1no! ho! many data bits and ho! many stop bits are being used.
$%s Complement0 negative integer is the complement of the positive integer plus one.
2xcess-&0 *dd & to the number, then represent by '-bit binary number.
)ray Code0 "tarting at /ero, ma1e one change in the least significant possible bit to ta1e you to the next state
This code has the advantage that arithmetic operations are straightfor!ard. "ubtraction is accomplished by binary addition of the $%s complement. ?ultiplication also can be carried out.
+ e Binary System
* pretty damn clear guide to a .uite confusing concept by Christine F. Kright !ith some help from "amuel *. Febels1y.
+able o) Contents
Basic Concepts Behind the Binary "ystem Binary *ddition Binary ?ultiplication Binary Division Conversion from Decimal to Binary @egation in the Binary "ystem
such that #6# is the hundreds column, #T# is the tens column, and #+# is the ones column. "o the number #(;&# is (-hundreds plus ;-tens plus &-ones. Lears later, !e learned that the ones column meant (858, the tens column meant (85(, the hundreds column (85$ and so on, such that
10^2|10^1|10^0 1 | 9 | 3
the number (;& is really N((D(85$ A(;D(85( A(&D(858 O. *s you 1no!, the decimal system uses the digits 8-; to represent numbers. ,f !e !anted to put a larger number in column (85n (e.g., (8 , !e !ould have to multiply (8D(85n, !hich !ould give (85(nA( , and be carried a column to the left. For example, putting ten in the (858 column is impossible, so !e put a ( in the (85( column, and a 8 in the (858 column, thus using t!o columns. T!elve !ould be ($D(858, or (858((8A$ , or (85(A$D(858, !hich also uses an additional column to the left (($ . The binary system !or1s under the exact same principles as the decimal system, only it operates in base $ rather than base (8. ,n other !ords, instead of columns being
10^2|10^1|10^0
they are
2^2|2^1|2^0
,nstead of using the digits 8-;, !e only use 8-( (again, if !e used anything larger it !ould be li1e multiplying $D$5n and getting $5nA(, !hich !ould not fit in the $5n column. Therefore, it !ould shift you one column to the left. For example, #&# in binary cannot be put into one column. The first column !e fill is the right-most column, !hich is $58, or (. "ince &P(, !e need to use an extra column to the left, and indicate it as #((# in binary ((D$5( A ((D$58 . 2xamples0 Khat !ould the binary number (8(( be in decimal notationQ
Clic1 here to see the ans!er Try converting these numbers from binary to decimal0
(8 ((( (8(8(
((((8
2^4| 2^3| 2^2| 2^1| 2^0 | | | 1 | 0 | | 1 | 1 | 1 1 | 0 | 1 | 0 | 1 1 | 1 | 1 | 1 | 0
Femember0
Binary Addition
Consider the addition of decimal numbers0
23 +48 ___
Ke begin by adding &A-I((. "ince (( is greater than (8, a one is put into the (8%s column (carried , and a ( is recorded in the one%s column of the sum. @ext, add N($A' A(O (the one is from the carry I4, !hich is put in the (8%s column of the sum. Thus, the ans!er is 4(. Binary addition !or1s on the same principle, but the numerals are different. Begin !ith one-bit binary addition0
0 +0 ___ 0 0 +1 ___ 1 1 +0 ___ 1
(A( carries us into the next column. ,n decimal form, (A(I$. ,n binary, any digit higher than ( puts us a column to the left (as !ould (8 in decimal notation . The decimal number #$# is !ritten in binary notation as #(8# ((D$5( A(8D$58 . Fecord the 8 in the ones column, and carry the ( to the t!os column to get an ans!er of #(8.# ,n our vertical notation,
1 +1 ___ 10
"tep one0 Column $580 8A(I(. Fecord the (. Temporary Fesult0 (R Carry0 8
"tep t!o0 Column $5(0 (A(I(8. Fecord the 8, carry the (. Temporary Fesult0 8(R Carry0 ( "tep three0 Column $5$0 (A8I( *dd ( from carry0 (A(I(8. Fecord the 8, carry the (. Temporary Fesult0 88(R Carry0 ( "tep four0 Column $5&0 (A(I(8. *dd ( from carry0 (8A(I((. Fecord the ((. Final result0 ((88(
*lternately0
11 (carry) 1010 +1111 ______ 11001
Al,ays remember
Binary (ultiplication
?ultiplication in the binary system !or1s the same !ay as in the decimal system0
101
@ote that multiplying by t!o is extremely easy. To multiply by t!o, =ust add a 8 on the end. Feturn to Table of Contents
Binary Di-ision
Follo! the same rules as in decimal division. For the sa1e of simplicity, thro! a!ay the remainder. For 2xample0 (((8((G((
10011 r 10 _______ 11)111011 11 ______ 101 11 ______ 101 11 ______ 10
Decimal to Binary
Converting from decimal to binary notation is slightly more difficult conceptually, but can easily be done once you 1no! ho! through the use of algorithms. Begin by thin1ing of a fe! examples. Ke can easily see that the number &I $A(. and that this is e.uivalent to ((D$5( A((D$58 . This translates into putting a #(# in the $5( column and a #(# in the $58 column, to get #((#. *lmost as intuitive is the number :0 it is obviously 'A(, !hich is the same as saying S($D$ A(T, or $5$A(. This can also be !ritten as S((D$5$ A((D$58 T. 3oo1ing at this in columns,
2^2 | 2^1 | 2^0 1 0 1
or (8(. Khat !e%re doing here is finding the largest po!er of t!o !ithin the number ($5$I' is the largest po!er of $ in : , subtracting that from the number (:-'I( , and finding the largest po!er of $ in the remainder ($58I( is the largest po!er of $ in ( . Then !e =ust put this into columns. This process continues until !e have a remainder of 8. 3et%s ta1e a loo1 at ho! it !or1s. Ke 1no! that0
2^0!1 2^1!2
and so on. To convert the decimal number 4: to binary, !e !ould find the largest po!er of $ less than 4:, !hich is 9'. Thus, !e !ould put a ( in the $59 column, and subtract 9' from 4:, giving us ((. The largest po!er of $ in (( is -, or $5&. Cut ( in the $5& column, and 8 in $5' and $5:. "ubtract - from (( to get &. Cut ( in the $5( column, 8 in $5$, and subtract $ from &. Ke%re left !ith (, !hich goes in $58, and !e subtract one to get /ero. Thus, our number is (88(8((. ?a1ing this algorithm a bit more formal gives us0 (. 3et DInumber !e !ish to convert from decimal to binary $. Fepeat until DI8
" " "
a. Find the largest po!er of t!o in D. 3et this e.ual C. b. Cut a ( in binary column C. c. "ubtract C from D.
&. Cut /eros in all columns !hich don%t have ones. This algorithm is a bit a!1!ard. Carticularly step &, #filling in the /eros.# Therefore, !e should re!rite it such that !e ascertain the value of each column individually, putting in 8%s and (%s as !e go0 (. 3et DI the number !e !ish to convert from decimal to binary $. Find C, such that $5C is the largest po!er of t!o smaller than D. &. Fepeat until CU8
"
,f $5CUID then
"
2lse
" "
@o! that !e have an algorithm, !e can use it to convert numbers from decimal to binary relatively painlessly. 3et%s try the number DI::.
+ur first step is to find C. Ke 1no! that $5'I(9, $5:I&$, and $59I9'. Therefore, CI:.
"ubtracting ::-&$ leaves us !ith $&. "ubtracting ( from C gives us '. Follo!ing step & again, $5'UI$&, so !e put a ( in the $5' column0 11 @ext, subtract (9 from $&, to get 4. "ubtract ( from C gives us &. $5&P4, so !e put a 8 in the $5& column0 110 @ext, subtract ( from C, !hich gives us $. $5$UI4, so !e put a ( in the $5$ column0 1101 "ubtract ' from 4 to get &. "ubtract ( from C to get (. $5(UI&, so !e put a ( in the $5( column0 11011 "ubtract $ from & to get (. "ubtract ( from C to get 8. $58UI(, so !e put a ( in the $58 column0 110111 "ubtract ( from ( to get 8. "ubtract ( from C to get -(. C is no! less than /ero, so !e stop. .
!here each aSiT is either a ( or a 8 (the only possible digits for the binary system . The only !ay a number can be odd is if it has a ( in the $58 column, because all po!ers of t!o greater than 8 are even numbers ($, ', -, (9... . This gives us the rightmost digit as a starting point. @o! !e need to do the remaining digits. +ne idea is to #shift# them. ,t is also easy to see that multiplying and dividing by $ shifts everything by one column0 t!o in binary is (8, or ((D$5( . Dividing ((D$5( by $ gives us ((D$58 , or =ust a ( in binary. "imilarly, multiplying by $ shifts in the other direction0 ((D$5( D$I((D$5$ or (8 in binary. Therefore
&a#n$*2^n + a#n 1$*2^(n 1) + %%% + a#1$*2^1 + a#0$*2^0'(2
is e.ual to
a#n$*2^(n 1) + a#n 1$*2^(n 2) + %%% + a#1$2^0
3et%s loo1 at ho! this can help us convert from decimal to binary. Ta1e the number (9&. Ke 1no! that since it is odd, there must be a ( in the $58 column (aS8TI( . Ke also 1no! that it e.uals (9$A(. ,f !e put the ( in the $58 column, !e have (9$ left, and have to decide ho! to translate the remaining digits. T!o%s column0 Dividing (9$ by $ gives -(. The number -( in binary !ould also have a ( in the $58 column. "ince !e divided the number by t!o, !e #too1 out# one po!er of t!o. "imilarly,
the statement aSn-(TD$5(n-( A aSn-$TD$5(n-$ A ... A aS(TD$58 has a po!er of t!o removed. +ur #ne!# $58 column no! contains a(. Ke learned earlier that there is a ( in the $58 column if the number is odd. "ince -( is odd, aS(TI(. Cractically, !e can simply 1eep a #running total#, !hich no! stands at (( (aS(TI( and aS8TI( . *lso note that a( is essentially #remultiplied# by t!o =ust by putting it in front of aS8T, so it is automatically fit into the correct column. Four%s column0 @o! !e can subtract ( from -( to see !hat remainder !e still must place (-8 . Dividing -8 by $ gives '8. Therefore, there must be a 8 in the '%s column, (because !hat !e are actually placing is a $58 column, and the number is not odd . 2ight%s column0 Ke can divide by t!o again to get $8. This is even, so !e put a 8 in the -%s column. +ur running total no! stands at aS&TI8, aS$TI8, aS(TI(, and aS8TI(. Ke can continue in this manner until there is no remainder to place.
1% 2% D% )e*+, -"r.a/i0e *1i, a/2"ri*1.3 )e* D! *1e n4.5er 6e 6i,1 *" c"n7er* -r". 8eci.a/ *" 5inary% 9e:ea* 4n*i/ D!03 a) ;- D i, "88< :4* =1= in *1e /e-*.",* ":en c"/4.n< an8 ,45*rac* 1 -r".
5) ;- D i, e7en< :4* =0= in *1e /e-*.",* ":en c"/4.n% c) Di7i8e D 5y 2% >n8 9e:ea* ?"r *1e n4.5er 163< *1i, 6"r@, a, -"//"6,3 1% )e* D!163 2% 5) D i, "88< :4* a 1 in *1e 2^0 c"/4.n% A45*rac* 1 -r". D *" 2e* 162% c) Di7i8e D!162 5y 2% Te.:"rary 9e,4/*3 01 Be6 D!81 D 8"e, n"* eC4a/ 0< ," 6e re:ea* ,*e: 2% 2% 5) D i, "88< :4* a A45*rac* 1 -r". D *" c) Di7i8e D!80 5y Te.:"rary 9e,4/*3 11 D 8"e, n"* eC4a/ 0< ," 2% 1 in *1e 2^1 c"/4.n% 2e* 80% 2% Be6 D!40 6e re:ea* ,*e: 2%
5) D i, e7en< :4* a 0 in *1e 2^2 c"/4.n% c) Di7i8e D 5y 2% Te.:"rary 9e,4/*3011 Be6 D!20 2% 5) D i, e7en< :4* a 0 in *1e 2^3 c"/4.n% c) Di7i8e D 5y 2% Te.:"rary 9e,4/*3 0011 Be6 D!10 2% 5) D i, e7en< :4* a 0 in *1e 2^4 c"/4.n% c) Di7i8e D 5y 2% Te.:"rary 9e,4/*3 00011 Be6 D!5 2% a) D i, "88< :4* a 1 in *1e 2^5 c"/4.n% A45*rac* 1 -r". D *" 2e* 4% c) Di7i8e D 5y 2% Te.:"rary 9e,4/*3 100011 Be6 D!2 2% 5) D i, e7en< :4* a 0 in *1e 2^6 c"/4.n%
Be6 D!1
a) D i, "88< :4* a 1 in *1e 27 c"/4.n% A45*rac* 1 -r". D *" 2e* D!0% c) Di7i8e D 5y 2% Te.:"rary 9e,4/*3 10100011 Be6 D!0 D!0< ," 6e are 8"ne< an8 *1e 8eci.a/ n4.5er 163 i, eC4i7a/en* *" *1e 5inary n4.5er 10100011%
"ince !e already 1ne! ho! to convert from binary to decimal, !e can easily verify our result. (8(888((I((D$58 A((D$5( A((D$5: A((D$54 I(A$A&$A($-I (9&. Feturn to Table of Contents
These techni.ues !or1 !ell for non-negative integers, but ho! do !e indicate negative numbers in the binary systemQ Before !e investigate negative numbers, !e note that the computer uses a fixed number of #bits# or binary digits. *n --bit number is - digits long. For this section, !e !ill !or1 !ith - bits. "igned ?agnitude0 The simplest !ay to indicate negation is signed magnitude. ,n signed magnitude, the left-most bit is not actually part of the number, but is =ust the e.uivalent of a AG- sign. #8# indicates that the number is positive, #(# indicates negative. ,n - bits, 8888((88 !ould be ($ (brea1 this do!n into ((D$5& A ((D$5$ . To indicate -($, !e !ould simply put a #(# rather than a #8# as the first bit0 (888((88. +ne%s Complement0 ,n one%s complement, positive numbers are represented as usual in regular binary. 6o!ever, negative numbers are represented differently. To negate a number, replace all /eros !ith ones, and ones !ith /eros - flip the bits. Thus, ($ !ould be 8888((88, and -($ !ould be ((((88((. *s in signed magnitude, the leftmost bit indicates the sign (( is negative, 8 is positive . To compute the value of a negative number, flip the bits and translate as before. T!o%s Complement0 Begin !ith the number in one%s complement. *dd ( if the number is negative. T!elve !ould be represented as 8888((88, and -($ as ((((8(88. To verify this, let%s subtract ( from ((((8(88, to get ((((88((. ,f !e flip the bits, !e get 8888((88, or ($ in decimal.
,n this notation, #m# indicates the total number of bits. For us (!or1ing !ith - bits , it !ould be excess $54. To represent a number (positive or negative in excess $54, begin by ta1ing the number in regular binary representation. Then add $54 (I($- to that number. For example, 4 !ould be ($- A 4I(&:, or $54A$5$A$5(A$58, and, in binary,(8888(((. Ke !ould represent -4 as ($--4I($(, and, in binary, 8((((88(. @ote0
Hnless you 1no! !hich representation has been used, you cannot figure out the value of a number. * number in excess $5(m-( is the same as that number in t!o%s complement !ith the leftmost bit flipped.
To see the advantages and disadvantages of each method, let%s try !or1ing !ith them. Hsing the regular algorithm for binary adition, add (:A($ , (-:A($ , (-($A-: , and (($A-($ in each system. Then convert bac1 to decimal numbers.
Ans,ers
. at ,ould t e binary number 1/11 be in decimal notation0
1011!(1*2^3)+(0*2^2)+(1*2^1)+(1*2^0) ! (1*8) + (0*4) + (1*2) + (1*1) ! 11 (in 8eci.a/ n"*a*i"n)
111 0
Clic1 here to return to the .uestion Hsing the regular algorithm for binary adition, add (:A($ , (-:A($ , (-($A-: , and (($A-($ in each system. Then convert bac1 to decimal numbers.
Ai2ne8 Da2ni*48e3 5+12 00000101 00001100 __________ 00010001 17 One+ E".:/e.en*3 00000101 00001100 _________ 00010001 17 T6"+, E".:/e.en*3 00000101 00001100 ________ 00010001 17 Ai2ne8 Da2ni*48e3 10000101 10001100 ________ 00010001 109 01111011 10001100 ________ 00000111 119 01110100 01111011 ________ 11101111 111 00001100 01110100 ________ 01111100 124 11111011 00001100 ________ 00000111 7 11110100 11111011 ________ 11101111 17 00001100 11110100 ________ 00000000 0 11111010 00001100 ________ 00000110 6 11110011 11111010 ________ 11101101 18 00001100 11110011 ________ 11111111 0 5+12 10000101 00001100 ________ 10010001 17 12+ 5 10001100 10000101 ________ 00010000 16 12+ 12 00001100 10001100 _________ 10011000 24