Binary number, Bits and Byte Sen Zhang. Number systems –Decimal –Binary –Bits –bytes...

78
Binary number, Bits and Byte Sen Zhang
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    249
  • download

    5

Transcript of Binary number, Bits and Byte Sen Zhang. Number systems –Decimal –Binary –Bits –bytes...

Binary number, Bits and Byte

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

• The decimal system is the base-10 system that we use every day.

• A number, say 6357, represented in the base-10 system consists of multiple ordered digits. (In other words, digits are normally combined together in groups to create larger numbers.)

• A digit is a single place that can hold numerical values between 0 and 9 (10 different values).

Let us start from an arbitrary decimal number

• For example, 6,357 has four digits.

• It is understood that in the number 6,357, – the 7 is filling the "1s place," – while the 5 is filling the 10s place, – the 3 is filling the 100s place – and the 6 is filling the 1,000s place.

• So you could express 6,357 this way if you want to be explicit:

(6 * 1000) + (3 * 100) + (5 * 10) + (7 * 1) = 6000 + 300 + 50 + 7 = 6357

10^3

Continue ..

• Another way to express it would be to use the concept of powers of 10.

• A specific digit is associated with a specific weight expressed as powers of 10. The first digit (counting from the right) gives 10 to the 0 power, the second digit gives 10 to the 1 power, and so on.

• Exponents are a shorthand way to show how many times a number, called the base, is multiplied times itself. A number with an exponent is said to be "raised to the power" of that exponent.

• Assuming that we are going to represent the concept of "raised to the power of" with the "^" symbol.

• "10 squared“ or 10 to the power of 2 is written as "10^2"

• 10 to the fourth power is denoted 10^4

• Thus, another way to express the previous number is like this: (6 * 10^3) + (3 * 10^2) + (5 * 10^1) + (7 * 10^0)

= 6000 + 300 + 50 + 7

= 6357

• What you can see from this expression is that each digit is a placeholder for the power of the index of that placeholder of base 10, starting from the least significant digit with 10 raised to the power of zero (i.e. counting from the rightmost digit).

• But why do we human beings use 10 based number system?

• The most commonly accepted explanation is that our base-10 number system was adopted by our ancestors most likely because we have 10 fingers.

• Interestingly enough, maybe that is why digit in English also means a finger or toe.

• We have reasons to ask a question in our minds: – “If we happened to evolve to have eight

fingers instead, would we probably have a base-8 number system?”

• The answer is probably YES!

Any other number systems?• The good news about number systems is

that it is not the only choice to have 10 different values in a digit.

• Actually, we can have base-anything number systems from a theoretical point of view.

• There are many good reasons to use different bases in different situations. For example, 7 days/week, 12 months/year

A generalized rule

• The following rules apply to base 10 and to any other base number system:– The system of base n requires n different

symbols or values. – The left most digit is the highest-order digit

and represents the most significant digit, while the lowest-order digit is the least significant digit.

– A digit is represented as powers of the system's base.

• Computers happen to operate using the base-2 number system, also known as the binary number system, just like the base-10 number system is known as the decimal number system to human beings.

The fundamental point

• Modern computers use binary number system, in which there are only zeros and ones. (Only two symbols)

• A “bit” to binary is similar a “digit” to a decimal information. (Again, the easiest way to understand bits is to compare them to something you know: digits.)

• A bit has a single binary value, either 0 or 1.

Binary vs. Decimal

• Binary is a base two system which works just like our decimal system.

• Considering the decimal number system, it has a set of values which range from 0 to 9.

• The binary number system is base 2 and therefore requires only two digits, 0 and 1.

The fundamental point

• Binary representation of numbers and other information is the representation which can be understood by computer chips and can be saved in memory.

• It is important to computers because all computer data is ultimately represented by a series of zeros and ones, no matter you realize it or not.

You might ask

• Why don’t computers use the base-10 decimal system for numbers, counting and arithmetic?

• Why not 4 based, 7 based?

• Why 2 based?

• We know that the computer doesn't have a real brain inside. In fact, it is made up mostly of semiconductor materials such as silicon. Yet, a computer acts in many ways as if it does have a real brain, because it can store (memorize) data and derive new information (operations) from the input data.

Why binary?

• These questions can be answered by a series of relevant questions! – How to store the values in hardware? – How to automatically perform arithmetic

operations on numbers? – …

• The fundamental question is can we find out a physical material to stably maintain n different status?

How to store?

• Advancement in material science guarantees that binary status can be represented with no ambiguity.

• Silicon and many other semiconductor materials can present one of two status at any given time, and can retain a status for a long time.

• Positive or negative, +5 volt or -5 volt.• Think about 2 status in electronic world, if not

One then Zero, very simple to implement in electronic world.

• One the other hand, it is difficult, if not impossible, to find out a material to be able to maintain 10 different status stably.

• Generally speaking, the more status to maintain, the more difficult to find out such a material.

How to calculate

• Another factor is how to implement proper digital circuits to perform arithmetic and logical operations based on a specific number system.

• It turns out that the binary system is the preferred way to implement CPUs to do various operations (arithmetic and logical operations). Not any other systems!

• You could wire up and build computers that operate in base-10 (assume physically we can find out such kind of material.), but they would be fiendishly expensive right now. On the other hand, base-2 computers are relatively cheap.

• Also because there must always be at least two symbols for an information processing system to be able to distinguish significances of different values and to manipulate on them, binary is the smallest numbering system that supports definite arithmetic and logic operations.

The simplest answer is

• Basically speaking, binary system simplifies information representation and information processing in electronic world.

• Binary number system is the easiest one to implement from the hardware point of view.

• The binary number system suits a computer extremely well, because it allows simple CPU and memory designs.

• So computers use binary numbers.

• Their CPU and memory are made up of millions of tiny “switches” that can be either ON or OFF. Two symbols, 0 and 1, can be used to stand for the two states of ON and OFF.

• Since the computer is really made up of tiny switches that can be either OFF or ON, you can look at a binary number as a series of light switches. A 1 represents a switch that is ON, and a 0 means a switch that is OFF.

• The computer's CPU needs only recognize two states, in the same way as a switch must always be open or closed, or an electrical flow on or off, a binary digit must always be one or zero. – on or off, – Yes or no,– 1 or 0.

• But from this on-off, yes-no 1-0 state, all things may can be represented completely and calculated correctly.

What is a feasible number system?

• Should be able to represent any information.• Should be able to support arithmetic and logic

operations.

Bits• The binary number system uses binary

digits (bits) in place of decimal digits.

• A binary number is composed of only 0s and 1s, like this: 1011.

• How do you figure out what the value of the binary number 1011 is in decimal world?

How does it work?

• As we have shown that our decimal system is based on place or location. That is, the place of each digit decides the value of that digit.

• The binary system works in exactly the same way, except that its place value is based on the number two.

• Therefore we have the one's place, the two's place, the four's place, the eight's place, the sixteen's place, and so on. Each place in the number represents two times (2X's) the place to its right.

• An example (1 * 2^3) + (0 * 2^2) + (1 * 2^1) + (1 * 2^0) = 8 + 0 + 2 + 1 = 11

How you count or add in decimal?

• Considering the decimal number system, it has a set of values which range from 0 to 9.

• If you add 1 to 9, carry will happen.• X + Y is greater than 10 and carry 1 onto the

next column on the left. • If the sum is less than 10, put it down at the

bottom and set the carry to zero. If it is not less than the base, subtract 10, put down the result, and set the carry to one.

• With only two numerals, 1 (one) and 0 (zero), counting in binary is pretty simple. Just keep in mind the following:

• 0 + 0 = 0 • 0 + 1 = 1 • 1 + 0 = 1 • 1 + 1 = 10 • When you look at this sequence,

– 0 and 1 are the same for decimal and binary number systems. – As for the decimal number 2, you see for the first time carrying

takes place in the binary system. If a bit is 1, and you add 1 to it, the bit becomes 0 and the next bit becomes 1.

Bits• 0 = 0 • 1 = 1 • 2 = 10 • 3 = 11 • 4 = 100 • 5 = 101 • 6 = 110 • 7 = 111 • 8 = 1000 • 9 = 1001 • 10 = 1010 • 11 = 1011 • 12 = 1100

Bits

• Starting at zero and going through 12, counting in decimal and binary have been listed on the previous slide.

• Please also notice that, in the transition from decimal 11 to decimal 12, the carrying effect rolls over through 2 bits, turning 1011 into 1100.

decimal to binary• Keep dividing by 2

• Ex 2 : 23710     

237 / 2 = 118    Remainder 1------------------------------------------------------| 118 / 2 = 59 Remainder 0---------------------------------------------------| |59 / 2 = 29 Remainder 1------------------------------------------------| | |29 / 2 = 14 Remainder 1------------------------------------------------| | |14 / 2 = 7 Remainder 0---------------------------------------------| | | |7 / 2 =3 Remainder 1------------------------------------------| | | | |3 / 2 = 1 Remainder 1-----------------------------------| | | | | | |1 / 2 = 0 Remainder 1--------------------------------| | | | | | | |                                          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 <->binarybinary 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.

• Octal to hexadecimal conversion, or visa versa, is most easily performed by first converting to binary.

• A binary number is converted to octal by grouping the bits in groups of three.

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 wordSystem 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.

• Hint: you can open and close your eyes to code different information.

• With 8 bits in a byte, you can represent 256 values ranging from 0 to 255, as shown here: 0 = 00000000

1 = 00000001

2 = 00000010

...

254 = 11111110

255 = 11111111

• This is related to ASCII code!

ASCII

• It is an acronym for the American Standard Code for Information Interchange.

• It is a standard seven-bit code that was first proposed by the American National Standards Institute or ANSI in 1963, and finalized in 1968 as ANSI Standard X3.4.

• The purpose of ASCII was to provide a standard to code various symbols ( visible and invisible symbols)

ASCII

• In the ASCII character set, each binary value between 0 and 127 represents a specific character.

• Most computers extend the ASCII character set to use the full range of 256 characters available in a byte. The upper 128 characters handle special things like accented characters from common foreign languages.

• In general, ASCII works by assigning standard numeric values to letters, numbers, punctuation marks and other characters such as control codes.

• An uppercase "A," for example, is represented by the decimal number 65."

Bytes: ASCII

• By looking at the ASCII table, you can clearly see a one-to-one correspondence between each character and the ASCII code used.

• For example, 32 is the ASCII code for a space. • We could expand these decimal numbers out to

binary numbers (so 32 = 00100000), if we wanted to be technically correct -- that is how the computer really deals with things.

Bytes: ASCII

• Computers store text documents, both on disk and in memory, using these ASCII codes.

• For example, if you use Notepad in Windows XP/2000 to create a text file containing the words, "Four score and seven years ago," Notepad would use 1 byte of memory per character (including 1 byte for each space character between the words -- ASCII character 32).

• When Notepad stores the sentence in a file on disk, the file will also contain 1 byte per character and per space.

• Binary number is usually displayed as Hexadecimal to save display space.

• Take a look at a file size now.

• Take a look at the space of your p drive

Bytes: ASCII

• If you were to look at the file as a computer looks at it, you would find that each byte contains not a letter but a number -- the number is the ASCII code corresponding to the character (see below). So on disk, the numbers for the file look like this:

• F o u r a n d s e v e n • 70 111 117 114 32 97 110 100 32 115 101

118 101 110

• Externally, it appears that human beings will use natural languages symbols to communicate with computer.

• But internally, computer will convert everything into binary data.

• Then process all information in binary world.• Finally, computer will convert binary information

back to symbols understandable to human beings .

• When you type the letter A, the hardware logic built into the keyboard automatically translates that character into the ASCII code 65, which is then sent to the computer. Similarly, when the computer sends the ASCII code 65 to output devices, the output hardware instead draw letter “A” on your screen or your computer.

revisit “char” data type

• In C++, single characters are represented using the data type char, which is one of the most important scalar data types.char achar;

achar=‘A’;

achar=65;

More on book

• Page 6 – basic idea

• Page 31- collating sequence

Character and integer

• A character and an integer (actually a small integer spanning only 8 bits) are actually indistinguishable on their own. If you want to use it as a char, it will be a char, if you want to use it as an integer, it will be an integer, as long as you know how to use proper C++ statements to express your intentions.

• 1 bit

• 1 byte = 8 bits

• 1 kb = 210 bytes = 1024 bytes !=1000

• 1 Mb = 1 k k bytes = 210 * 210 bytes

• 1 G b = 210 * 210 * 210 bytes

• 1 Terab = 210 * 210 * 210 * 210 bytes

Even larger capacity

• 1 petabyte = 210 * 210 * 210 * 210 * 210 bytes (2 to the 50th power )

• 1 exabyte= 260

• 1 zettabyte = 270

• 1 yottabyte = 280

Some interesting facts about what these various-sized bytes can store:

• 1 bit: a binary decision• 1 byte: a character• 5 Megabytes: The complete works of Shakespeare• 2 Gigabytes: 20 meters of shelved books• 10 Terabytes: The printed collection of the US Library of

Congress• 200 Petabytes: All printed material in the whole word.• 5 Exabytes: All words ever spoken by human beings

CPU processes binary number

• The first microprocessor to make it into a home computer was the Intel 8080, a complete 8-bit computer on one chip, introduced in 1974.

• PC market moved from the 8088 to the 80286, the 80386, 80486, the Pentium, the Pentium II to the Pentium III to the Pentium 4.

• All of these microprocessors are made by Intel and all of them are improvements on the basic design of the 8088.

• The Pentium 4 can execute any piece of code that ran on the original 8088, but it does it about 5,000 times faster!

END