Computer Science 101 Binary Systems
Humans Decimal Numbers (base 10) Decimal Numbers (base 10) Sign-Magnitude (-324) Sign-Magnitude (-324) Decimal Fractions (23.27) Decimal Fractions (23.27) Letters for text Letters for text
Computers Binary Numbers (base 2) Binary Numbers (base 2) Two’s complement and sign-magnitude Two’s complement and sign-magnitude Binary fractions and floating point Binary fractions and floating point ASCII codes for characters (A 65) ASCII codes for characters (A 65)
Why binary? Information is stored in computer via voltage levels. Information is stored in computer via voltage levels. Using decimal would require 10 distinct and reliable levels for each digit. Using decimal would require 10 distinct and reliable levels for each digit. This is not feasible with reasonable reliability and financial constraints. This is not feasible with reasonable reliability and financial constraints. Everything in computer is stored using binary: numbers, text, programs, pictures, sounds, videos,... Everything in computer is stored using binary: numbers, text, programs, pictures, sounds, videos,...
How can that be? Everything in computer is stored using binary: numbers, text, programs, pictures, sounds, videos,... Everything in computer is stored using binary: numbers, text, programs, pictures, sounds, videos,...
Transistor A transistor is an electronic switch A transistor is an electronic switch Basic unit of modern computer storage Basic unit of modern computer storage Two steady states based on voltage levels Two steady states based on voltage levels Say, 500 million transistors on a chip 1 cm 2 Say, 500 million transistors on a chip 1 cm 2 Change states in billionth of sec Change states in billionth of sec Solid state Solid state
Morse Code Morse Code
Morse Code Tree
Decimal: Non-negatives Base 10 Base 10 Uses decimal digits: 0,1,2,3,4,5,6,7,8,9 Uses decimal digits: 0,1,2,3,4,5,6,7,8,9 Positional System - position gives power of the base Positional System - position gives power of the base Example: 3845 = 3x x x x10 0 Example: 3845 = 3x x x x10 0 Positions: … Positions: …543210
Binary: Non-negatives Base 2 Base 2 Uses binary digits (bits): 0,1 Uses binary digits (bits): 0,1 Positional system Positional system Example: 1101 = 1x x x x2 0 Example: 1101 = 1x x x x2 0
Conversions ExternalInternal (Human) (Computer) A ExternalInternal (Human) (Computer) A Humans want to see and enter numbers in decimal. Humans want to see and enter numbers in decimal. Computers must store and compute with bits. Computers must store and compute with bits.
Binary to Decimal Conversion Algorithm: Algorithm: Expand binary number using positional scheme.Expand binary number using positional scheme. Perform computation using decimal arithmetic.Perform computation using decimal arithmetic. Example: 1x x x x x2 0 = = = Example: 1x x x x x2 0 = = = 25 10
Decimal to Binary - Algorithm 1 Algorithm: While N 0 do Set N to N/2 (whole part) Record the remainder (1 or 0) end-of-loop Set A to remainders in reverse order Algorithm: While N 0 do Set N to N/2 (whole part) Record the remainder (1 or 0) end-of-loop Set A to remainders in reverse order
Decimal to binary - Example Example: Convert to binary N Rem N Rem Example: Convert to binary N Rem N Rem = =
Decimal to Binary - Algorithm 2 Algorithm: Set A to 0 (all bits 0) While N 0 do Find largest P with 2 P N Set bit in position P of A to 1 Set N to N - 2 P end-of-loop Algorithm: Set A to 0 (all bits 0) While N 0 do Find largest P with 2 P N Set bit in position P of A to 1 Set N to N - 2 P end-of-loop
Decimal to binary - Example Example: Convert to binary N Power P A Example: Convert to binary N Power P A = =
Binary Addition One bit numbers: | | 1 10 One bit numbers: | | 1 10 Example (53) (45) (98) Example (53) (45) (98)
Octal Numbers Base 8 Digits 0,1,2,3,4,5,6,7 Base 8 Digits 0,1,2,3,4,5,6,7 Number does not have so many digits as binary Number does not have so many digits as binary Easy to convert to and from binary Easy to convert to and from binary Often used by people who need to see the internal representation of data, programs, etc. Often used by people who need to see the internal representation of data, programs, etc.
Octal Conversions Octal to Binary Octal to Binary Simply convert each octal digit to a three bit binary number. Simply convert each octal digit to a three bit binary number. Example: = Example: = Binary to Octal Binary to Octal Starting at right, group into 3 bit sections Starting at right, group into 3 bit sections Convert each group to an octal digit Convert each group to an octal digit Example = = Example = =
Hexadecimal Base 16 Digits 0,…,9,A,B,C,D,E,F Base 16 Digits 0,…,9,A,B,C,D,E,F Hexadecimal Binary Hexadecimal Binary Just like Octal, only use 4 bits per digit. Just like Octal, only use 4 bits per digit. Example: 98C3 16 = Example: 98C3 16 = Example = = 34EB Example = = 34EB
Python example