Professional Documents
Culture Documents
Sen Zhang
• Number systems
– Decimal
– Binary
– Bits
– bytes
– Hexadecimal
– Octal
– Numbers conversion among different systems
• Ascii code
Bits & Bytes?
• Have you ever heard of words bits and bytes?
Have you heard of an advertisement that says,
"This computer has a 64-bit Pentium IV
processor with 256 mega-bytes of RAM and
100 giga-bytes of hard disk space."
• Probably the answer is yes, even for a normal
computer user, not to mention you, a C++
programmer.
• As a computer programmer, you should know
what bits and bytes are and how to work with
numbers expressed in binary and hexadecimal
notations.
• In this lecture, we will discuss bits and
bytes, binary and decimal numbers in
detail so that you will gain a fundamental
understanding about their meanings, what
these systems are and how they work.
• To help you understand, let's first review
the well known decimal number system.
The Decimal Number System
• Ex 2 : 23710
v v v v v v v v
1 1 1 0 1 1 0 1
Binary arithmetic operation
• Look at adder in binary and decimal
3
+3
=6
11
+ 11
=110 (carry) which is 6 in decimal.
How to add two numbers which are
not necessarily base 10
• Start with the rightmost column of digits (be sure the numbers are
properly aligned with units digits under each other).
• Begin with carry zero.
• Add the digits in the current column plus the carry.
• If the sum is less than the base, put it down at the bottom and set
the carry to zero. If it is not less than the base, subtract the base, put
down the result, and set the carry to one.
• If you are not out of columns, move to the next one to the left, and
go back to step c above.
• If the carry is not zero, write it down as the leftmost digit of the sum.
• Stop.
More binary operations
• Likewise, other arithmetic operations such as
subtraction, multiplication and division, as well
as other logical operations can all be
accomplished electronically in CPUs, but more
complicated than binary adder.
• You just need to know that binary numbers can
represent everything, support a complete set of
arithmetic and logic operations. (Not the
concentration of this class, take introduction to
computer science or architecture course if want
to explore more such as binary complementary
code etc.!)
• As you can see, numbers can become
rather long and appear to be cumbersome
in the binary system. For example, to
show the number 10, we need four light
switches, or four places.
• However, it is not a problem to computers
at all! Because the real switches inside a
computer are tiny and they are able to turn
on and off very rapidly.
The Hexadecimal System
• Although not a problem internally, long binary
number seems a problem to display in some
situations. A common practice to solve this
problem is to use hexadecimal to represent
Binary numbers more compactly externally.
• The hexadecimal system is base 16. Therefore,
it requires 16 different symbols. The values 0
through 9 are used, along with the letters A
through F, which represent the decimal values
10 through 15.
0..9, A, B, C, D, E, F
0..9, 10, 11, 12,13, 14, 15
Hexadecimal <->binary
binary Hexadecimal
0000 0
0001 1
0010 2
0011 3
0100 4
0101 5
0110 6
0111 7
1000 8
1001 9
1010 A
1011 B
1100 C
1101 D
1110 E
1111 F
Hex decimal binary
10 16 10000
F0 240 11110000
FF 255 11111111
Hexadecimal <->binary
• Group binary number 4 by 4 starting from
the least significant position.
11,1101,1001 3D9
1 1 1 0 ,1 1 0 1 ED
The Octal System
• The Octal system is base 8. Therefore it
requires 8 digits. The values 0 through 7
are used.
1 1 ,1 0 1 , 1 0 1 355
• The binary, hexadecimal (hex) and octal
system share one common feature – they
are all based on powers of 2.
• Each digit in the hex system is equivalent
to a four-digit binary number and each
digit in the octal system is equivalent to a
3-digit binary number.
1 bit
1 byte
4 bytes = 1 word
System dependent.
A bit
• A ‘bit’ (from Binary + digIT) is the smallest
unit of memory, also the unit of
measurement of data information.
Bytes
• Since a single bit holds so little
information, bits are rarely seen alone in
computers. They are almost always
bundled together into 8-bit collections, and
these collections are called bytes.
• Bytes, larger units, then are treated as
integral units of storage.
Words
• On most machines, “bytes” are assembled
into larger structures called “words”, where
a word is usually defined to be the size
required to hold an integer value.
• Some machines use two-byte words (16
bits), while some others use 4-byte
words(32 bits) and some machines use
less conventional sizes.
Why are there 8 bits in a byte?
• A similar question is, "Why are there 12 eggs in a
dozen?"
• Why your table has no larger or smaller working area?
• It targets at the most common situation.
• The 8-bit byte is something that people settled on
through trial and error over the past 50 years.
• To some extend, 8-bit is enough to represent all English
characters and Arabic numbers. A byte used to be the
basic unit to hold an individual character in a text
document.
One question?
• Can you use your one eye to show yes or
no?
• Can you use your two eyes to represent
four directions, north, east, south and west
to your partner.