810 likes | 1.88k Vues
Binary number, Bits and Byte. Sen Zhang. Number systems Decimal Binary Bits bytes Hexadecimal Octal Numbers conversion among different systems Ascii code. Bits & Bytes?.
E N D
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).
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 <->binary • Group binary number 4 by 4 starting from the least significant position. 11,1101,1001 3D9 ED 1 1 1 0 ,1 1 0 1