1、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 proc
2、essor 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 hexadecima
3、l 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, lets first review the well known decimal number system.,The Dec
4、imal 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
5、 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
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,103,Continue ,Another way to express it would be to use the concept of powers of 10. A specific digit is associated with a specific we
7、ight 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
8、“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 “102“ 10 to the fourth power is denoted 104,Thus, another way to express the previous number is like this: (6
9、* 103) + (3 * 102) + (5 * 101) + (7 * 100) = 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 fr
10、om 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 o
11、r 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 differ
12、ent 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 ba
13、se 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 systems base.,Computers happen
14、 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
15、 “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. Consi
16、dering 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
17、 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 dont computers use the base-10 decimal system for numbers, counting and arithmetic? Why not 4 based, 7
18、 based? Why 2 based?,We know that the computer doesnt 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) fr
19、om 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
20、?,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
21、 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 difficu
22、lt 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
23、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 mus
24、t 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, bina
25、ry 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 computer
26、s 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 binar
27、y 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 computers 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
28、 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 s
29、ystem 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. Th
30、at 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 ones place, the twos place, the fours place, the eights place, the sixteens place, and so on. Each place in t
31、he number represents two times (2Xs) the place to its right. An example(1 * 23) + (0 * 22) + (1 * 21) + (1 * 20) = 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
32、 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 binar
33、y 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,
34、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
35、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 2Ex 2 : 23710 237 / 2 = 118 Remainder 1-| 118 / 2 = 59 Remainder 0-| | 59 / 2 = 29 Remainder 1-| | | 29 / 2 = 14 Remainder 1-|
36、| | 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 decimal3 + 3 = 611 + 11 =110 (carry) which is 6 in decimal.,How to add tw
37、o 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
38、 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.
39、,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 everythi
40、ng, 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 cumbersom
41、e 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 pro
42、blem 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
43、 9 are used, along with the letters A through F, which represent the decimal values 10 through 15. 09, A, B, C, D, E, F 09, 10, 11, 12,13, 14, 15,Hexadecimal binary,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 Oc
44、tal 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
45、 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 Syst
46、em 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 c
47、ollections 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
48、 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.,
copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1