ICS: Introduction to Computing Science Binary Number System Yes 40% ICS Class No No 60% Yes 2 Contents • 2.1 Binary Values and Number System • 2.2 Data and Computers • 2.3 Representing Numeric Data • 2.4 Representing Text • 2.5 Representing Audio Information • 2.6 Representing Image and Graphics • 2.7 Representing Video ICS Class 3 2.1 Binary Values and Number System • Number Categories – Number: A unit of an abstract mathematical system subject to the laws of arithmetic – Natural number: The number 0 and any number obtained by repeatedly adding 1 to it. – Negative number: A value less than 0, with a sign opposite to its positive counterpart – Integer: Any of the natural numbers or any of the negatives of these numbers – Rational number: An integer or the quotient of two integers (division by zero excluded) ICS Class https://en.wikipedia.org/wiki/Irrational_number 4 2.1 Binary Values and Number System • Natural Number 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 … 19 20 21 …… 101 102 103 …… …… • Decimal (base-10) • The base of a number system – specifies the number of digits used in the system • Base-8, base-5, ……, base-2 (binary) ICS Class 5 2.1 Binary Values and Number System • Positional Notation – Base-10 – Base-R ICS Class The rightmost digit represents its value times the base to the zeroth power. The digit to the left of that one represents its value times the base to the first power. The next digit represents its value times the base to the second power. The next digit represents its value times the base to the third power, and so on. Position value A more formal way of defining positional notation is that the value is represented as a polynomial in the base of the number system. 6 2.1 Binary Values and Number System • Positional Notation Example: – Base-10 943 9 ∗ 102 + 4 ∗ 101 + 3 – Base-13 ICS Class Keep in mind that these two numbers have an equivalent value. That is, they both represent the same number of things. If a bag contains 943 (base 13) beans, and another bag contains 1576 (base 10) beans, both bags contain the exact same number of beans. Number systems just allow us to represent values in various ways. 7 2.1 Binary Values and Number System • Binary, octal, and hexadecimal – Binary number system (Base-2) • 2 digits: 0-1 – Octal number system (Base-8) • 8 digits: 0-7 – Hexadecimal number system(Base-16) • 16 digits ICS Class 8 2.1 Binary Values and Number System • Binary, octal, and hexadecimal Examples: convert the octal number 754 to decimal Examples: convert the hexadecimal number ABC to decimal: ICS Class 9 2.1 Binary Values and Number System • Binary, octal, and hexadecimal Examples: convert the octal number 754 to decimal Examples: convert the hexadecimal number ABC to decimal: ICS Class 10 2.1 Binary Values and Number System • Binary, octal, and hexadecimal Examples: convert a binary (base-2) number 1010110 to decimal. ICS Class 11 2.1 Binary Values and Number System • Binary, octal, and hexadecimal Examples: convert a binary (base-2) number 1010110 to decimal. ICS Class 12 2.1 Binary Values and Number System • Binary, octal, and hexadecimal Exercise: convert the hexadecimal number 11D to decimal convert the binary number 110110 to decimal convert the octal number 172 to decimal convert the octal number 7801 to decimal ICS Class 13 2.1 Binary Values and Number System • Arithmetic in Other Bases – Addition The rightmost digit reverts to 0, and there is a carry into the next position to the left. – Subtraction To accomplish this, you have to ―borrow one from the next left digit of the number from which you are subtracting. More precisely, you borrow one power of the base. ICS Class minuend subtractor 14 2.1 Binary Values and Number System • Arithmetic in Other Bases Exercise 110011 + 110111 = 11100 – 10111 = ICS Class 15 2.1 Binary Values and Number System • Power of Two Number System – Binary and Octal – convert from binary to octal • Combine three digits as a group ,from right to left, then convert each group to a octal digit using the mapping relationship. ICS Class 16 2.1 Binary Values and Number System • Power of Two Number System – Binary and Hexadecimal – convert from binary to hexadecimal – convert hexadecimal to binary ICS Class 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 17 2.1 Binary Values and Number System • Converting from Base 10 to other Bases – Converting algorithm ICS Class 18 2.1 Binary Values and Number System • Converting from Base 10 to other Bases Examples: convert the decimal number 2748 to hexadecimal C B A – The final answer is ABC. ICS Class 19 2.1 Binary Values and Number System • Converting from Base 10 to other Bases Examples: convert the decimal number 148 to binary Examples: convert the decimal number 548 to octal ICS Class 20 Contents • 2.1 Binary Values and Number System • 2.2 Data and Computers • 2.3 Representing Numeric Data • 2.4 Representing Text • 2.5 Representing Audio Information • 2.6 Representing Image and Graphics • 2.7 Representing Video ICS Class 21 2.2 Data and Computers • Modern computers are binary machines – Modern computers are designed to use and manage binary values – each storage location within a computer either contains a low-voltage signal or a high-voltage signal – These devices (storage and management) only have to represent one of two possible values. – Therefore they reliable are far less expensive and far more – Each storage location can have one of two states, it is logical to equate those states to 0 and 1. ICS Class 22 2.2 Data and Computers • Modern computers are binary machines – There are many ways to implement two states, for example, low voltage/ high voltage, low current/high current. – In fact, modern computer adopts voltage signal. – In general, • A signal in the range of 0 to 2 volts is considered “low” and is interpreted as a binary 0. • A signal in the range of 2 to 5 volts is considered “high” and is interpreted as a binary 1. ICS Class 23 2.2 Data and Computers • Analog and Digital Information – Analog data is a continuous representation, analogous to the actual information it represents. mercury – Digital data is a discrete representation, breaking the information up into separate elements. • Computers cannot work well with analog information. So instead, we digitize information by breaking it into pieces and representing those pieces separately. Thermometer ICS Class 24 2.2 Data and Computers • Analog and Digital Information – All electronic signals degrade (退化) as they move down a line. That is, the voltage of the signal fluctuates due to environmental effects. – The trouble is as soon as an analog signal degrades, information is lost. ICS Class 25 2.2 Data and Computers • Analog and Digital Information – Digital signals jump sharply between two extremes. – A digital signal can degrade quite a bit before any information is lost. Because any voltage value above a certain threshold is considered a high value, and any value below that threshold is considered a low value. – Periodically, a digital signal is reclocked to regain its original shape. As long as it is reclocked before too much degradation occurs, no information is lost. ICS Class 26 2.2 Data and Computers • Modern computers are binary machines – Each storage unit is called a binary unit or bit for short. – Bits are grouped together into bytes (8bits) – Eight bits is one byte – Kilobyte: 1024 bytes = 210 bytes KB – Megabyte :1024 Kilobytes=220 bytes MB – Gigabyte: 1024 Megabytes = 230 bytes GB – Terabyte: 1024 Gigabyte = 240 bytes TB ICS Class 27 2.2 Data and Computers • Binary Representations – One bit can be either 0 or 1. There are no other possibilities. Therefore, one bit can represent only two things – To represent more than two things, we need multiple bits – In general, n bits can represent 2n things – How many bits do you need to represent, say, 27 unique things? ICS Class 28 Contents • 2.1 Binary Values and Number System • 2.2 Data and Computers • 2.3 Representing Numeric Data • 2.4 Representing Text • 2.5 Representing Audio Information • 2.6 Representing Image and Graphics • 2.7 Representing Video ICS Class 29 2.3 Representing Numeric Data • Representing Negative Number – Signed-Magnitude Representation ICS Class 30 2.3 Representing Numeric Data • Representing Negative Number – Fixed-Sized Numbers Addition Subtraction (Note that the carries are discarded) ICS Class 31 2.3 Representing Numeric Data • Representing Negative Number – Fixed-Sized Numbers • In last example, we have assumed a fixed size of 100 values, and kept our numbers small enough to use the number line to calculate the negative representation of a number. • This representation of negative numbers is called the ten’s complement. ICS Class 32 2.3 Representing Numeric Data • Representing Negative Number – Two’s Complement • The complement strategy is actually easier in some ways when it comes to electronic calculations. • Everything in a modern computer is stored in binary, we use the binary equivalent of the ten‘s complement, called the two’s complement. ICS Class 33 2.3 Representing Numeric Data • Representing Negative Number – Two’s Complement • Assume that a number must be represented in eight bits. • Addition and subtraction are accomplished the same way as in two‘s complement arithmetic: • Note that : with this representation, the leftmost bit in a negative number is always a 1. Therefore you can say that the leftmost position is a sign position. ICS Class 34 2.3 Representing Numeric Data • Representing Negative Number – Two’s Complement – The reasons why we use two’s complement 1)It has only one representation of zero. 2)Addition and subtraction are accomplished the same way. It is not easy for computer to do borrow. 3)The sign bit participates in the calculation. ICS Class 35 2.3 Representing Numeric Data • Representing Negative Number – Two’s Complement – Exercise: • 126 - 127 • -126 + 8 • -126 - 3 ICS Class Assume that we use eight bits to represent numbers 1. Convert the decimal number to binary 2. Use two’s complement representation to represent negative number 3. Do addition, discard the leftmost carry 4. Write the result down and check whether it is right 36 2.3 Representing Numeric Data • Representing Negative Number – Number Overflow • Overflow occurs when the value that we compute cannot fit into the number of bits we have allocated for the result. • For example, if each value is stored using eight bits, adding 127 to 3 would overflow: • 100000010 in our scheme represents -126, not +130. ICS Class 37 2.3 Representing Numeric Data • Representing Real Number Decimal point – In decimal 104.32 100s position 10s position 1s position 10-1s position tenth – In binary 100-1s position hundredth Radix point 101.01 22s position ICS Class 21 s position 20s position 2-1s position 2-2s position 38 2.3 Representing Numeric Data • Representing Real Number – How to convert a real number from decimal to binary? – eg. 4.125 – eg. 0.3 – Fixed point and floating point ICS Class 39 2.3 Representing Numeric Data • Representing Real Number – Floating point • In decimal -148.69 14869 *10 2 • In binary 1 bit 52 bits 11 bits 0 0000000000000000000000000000000000011100000000000000 00000000011 sign mantissa exp ICS Class 40 Contents • 2.1 Binary Values and Number System • 2.2 Data and Computers • 2.3 Representing Numeric Data • 2.4 Representing Text • 2.5 Representing Audio Information • 2.6 Representing Image and Graphics • 2.7 Representing Video ICS Class 41 2.4 Representing Text • To represent a text document in digital form, we simply need to be able to represent every possible character that appear. • There are a finite number of characters. • So the general approach for representing characters is to list them all and assign each character a binary string. • To store a particular letter, we store the appropriate bit string. • A character set is simply a list of characters and the codes used to represent each one. ICS Class 42 2.4 Representing Text • The ASCII Character Set (American Standard Code for Information Interchange) – The ASCII character set originally used seven bits to represent each character, allowing for 128 unique characters. – Later ASCII evolved so that all eight bits were used to represent a character. – This eight-bit version is formally called the Latin–1 Extended ASCII character set. – Then the extended ASCII set allows for 256 characters and includes accented letters as well as several additional special symbols. ICS Class 43 ICS Class 44 2.4 Representing Text • The Unicode Character Set – The Unicode character set uses 16 bits per character. – Therefore, the Unicode character set can represent 216, or over 65 thousand characters. The goal of the people who created Unicode is to represent every character in every language used in the entire world. ICS Class 45 2.4 Representing Text • The Unicode Character Set – The Unicode character set uses 16 bits per character. – For consistency, Unicode was designed to a superset of ASCII. The first 256 characters in the Unicode character set correspond exactly to the extended ASCII character set. – There are also other character set, such as • Big5 (traditional Chinese) • GBK (simple Chinese) • UTF-8 ICS Class https://baike.baidu.com/item/UTF-8/481798?fr=aladdin https://baike.baidu.com/item/GBK字库/3910360?fr=aladdin 46 2.4 Representing Text ICS Class 47 2.4 Representing Text ICS Class 48 2.4 Representing Text • Text Compression – Data compression—reducing the amount of space needed to store a piece of data. – The compression ratio gives an indication of how much compression occurs. – The compression ratio is the size of the compressed data divided by the size of the original data. – Lossless: the data can be retrieved without losing any of the original information. – Lossy: some information is lost in the process of compaction. ICS Class 49 2.4 Representing Text • Text Compression – Keyword Encoding • The thought of this method is that there are many words which frequently occur in our text document or language. For example, in English language, the word “the”, “and”, “that” occur very often. If we use a single character to replace this word, the file size will shrink. • Keyword Encoding is a text compression method, in which frequently used words are replaced with a single character. • To decompress the document, you reverse the process: replace the single characters with the appropriate full word. ICS Class 50 2.4 Representing Text Example: The original paragraph is: The human body is composed of many independent systems, such as the circulatory system, the respiratory system, and the reproductive system. Not only must all systems work independently, they must interact and cooperate as well. Overall health is a function of the well-being of separate systems, as well as how those separate systems work in concert. 351 characters The encoded paragraph is: 316 characters ICS Class The human body is composed of many independent systems, such ^ ~ circulatory system, ~ respiratory system, + ~ reproductive system. Not only & all systems work independently, they & interact + cooperate ^ %. Overall health is a function of ~ %-being of separate systems, ^ % ^ how # separate systems work in concert. 51 2.4 Representing Text • Text Compression – Keyword Encoding Example • There are a total of 351 characters in the original paragraph including spaces and punctuation. • The encoded paragraph contains 316 characters, resulting in a savings of 35 characters. • The compression ratio for this example is 316/351 or approximately 0.9. ICS Class 52 2.4 Representing Text • Text Compression – Keyword Encoding Characteristics • This method is not very efficient. The compression ratio is too high. • There is a limitation to use this compression method. We must make sure all single symbols we use to replace the full words can not appear in the document. ICS Class 53 2.4 Representing Text • Text Compression – Run-length Encoding • In some situations, a single character may be repeated over and over again in a long sequence. • A text compression technique called run-length encoding capitalizes on these situations. It is sometimes called recurrence coding. • In run-length encoding, a sequence of repeated characters is replaced by a flag character, followed by the repeated character, followed by a single digit that indicates how many times the character is repeated. ICS Class 54 2.4 Representing Text • Text Compression – Run-length Encoding • Example: consider the following string : AAAAAAA • If we use the ‘*’ character as our flag, this string would be encoded as: *A7 • The encoded string: *n5*x9ccc*h6 some other text *k8eee • would be decoded into the following original text: nnnnnxxxxxxxxxccchhhhhh some other text kkkkkkkkeee • The compression ratio in this example of 35/51 or approximately 0.68. ICS Class 55 2.4 Representing Text • Text Compression – Huffman Encoding • This approach is contrary to the idea of a character set, in which each character is represented by a fixed-length bit string (such as 8 or 16). • The idea behind this approach is that if we use only a few bits to represent characters that appear often and reserve longer bit strings for characters that don‘t appear often, the overall size of the document being represented is small. ICS Class 56 2.4 Representing Text • Text Compression – Huffman Encoding Encoding Example: – The word DOORBELL would be encoded in binary as: 1011110110111101001100100 – The compression ratio: – If we used a fixed-size bit string to represent each character (say, 8 bits), then the binary form of the original string would be 8 characters times 8 bits or 64 bits. – The Huffman encoding for that string is 25 bits long, giving a compression ratio of 25/64, or approximately 0.39. ICS Class 57 2.4 Representing Text • Text Compression – Huffman Encoding Encoding Example: – The word DOORBELL would be encoded in binary as: 1011110110111101001100100 – The compression ratio: – If we used a fixed-size bit string to represent each character (say, 8 bits), then the binary form of the original string would be 8 characters times 8 bits or 64 bits. – The Huffman encoding for that string is 25 bits long, giving a compression ratio of 25/64, or approximately 0.39. ICS Class 58 2.4 Representing Text • Text Compression – Huffman Encoding • Decoding Example: – 1010110001111011 – These characters are represented by different bit strings. When we do decoding, we don’t know how many bits we should include for each character. – It seems that we might get confused. Let us scan this bit string from left to right. – it must be decoded into the word BOARD. ICS Class 59 2.4 Representing Text • Text Compression – Huffman Encoding • Decoding Example: – 1010110001111011 – it must be decoded into the word BOARD. – An important characteristic of any Huffman encoding is that no bit string used to represent a character is the prefix of any other bit string used to represent a character. ICS Class https://blog.csdn.net/google19890102/article/details/54848262 60 Contents • 2.1 Binary Values and Number System • 2.2 Data and Computers • 2.3 Representing Numeric Data • 2.4 Representing Text • 2.5 Representing Audio Information • 2.6 Representing Image and Graphics • 2.7 Representing Video ICS Class 61 2.5 Representing Audio Information • Sound – A sound is defined in nature by the wave of air that interacts with our eardrum. – The sound wave is an analog signal . – To represent audio information on a computer, we must digitize the sound wave, somehow breaking it into discrete, manageable pieces. ICS Class 62 2.5 Representing Audio Information • Digitization – To digitize the signal we periodically measure the voltage of the signal and record the appropriate numeric value. This process is called sampling. – To reproduce the sound, the stored voltage values are used to create a new continuous electronic signal. ICS Class 63 2.5 Representing Audio Information • Sampling rate – A sampling rate of around 40,000 times per second is enough to create a reasonable sound – If the sampling rate is much lower than that, the human ear begins to hear distortions (失真/畸变). – A higher sampling rate produces better quality sound, but after a certain point the extra data is irrelevant because the human ear can‘t hear the difference. ICS Class 64 2.5 Representing Audio Information • A vinyl record album VS. Compact Disc (CD) – A vinyl record album is an analog representation of the sound wave. • The needle of a record player (turntable) rides up and down in the spiral groove of the album. • The rise and fall of the needle is analogous to the voltage changes of the signal that represents the sound. ICS Class 65 2.5 Representing Audio Information • A vinyl record album VS. Compact Disc (CD) – A compact disc (CD), stores audio information digitally. • On the surface of the CD are microscopic pits that represent binary digits. • For a CD player, a low intensity laser is pointed at the disc. The laser light reflects strongly if the surface is smooth and reflects poorly if the surface is pitted. • A receptor analyzes the reflection and produces the appropriate string of binary data. This data represents the numeric voltage values. Then the signal is reproduced and sent to the speaker. ICS Class 66 2.5 Representing Audio Information • MP3 (MPEG–2, audio layer 3 file) – MPEG is an acronym for the Moving Picture Experts Group, which is an international committee that develops standards for digital audio and video compression. – First it analyzes the frequency spread and compares it to mathematical models of human psychoacoustics (心理声 学), then it discards information that can‘t be heard by humans. – Then the bit stream is compressed using a form of Huffman encoding to achieve additional compression. ICS Class 67 Contents • 2.1 Binary Values and Number System • 2.2 Data and Computers • 2.3 Representing Numeric Data • 2.4 Representing Text • 2.5 Representing Audio Information • 2.6 Representing Image and Graphics • 2.7 Representing Video ICS Class 68 2.6 Representing Images and Graphics • Representing Color – Color is our perception of the various frequencies of light that reach the retinas (虹膜) of our eyes. – Our retinas have three types of color photoreceptor (感光器) cone cells that respond to different sets of frequencies. – Three photoreceptor categories correspond to the colors of red, green, and blue. – All colors perceptible by the human eye can be made by combining various amounts of these three colors. ICS Class 69 2.6 Representing Images and Graphics • Representing Color – Color is often expressed in a computer as an RGB (red-greenblue) value. – RGB value is actually three numbers that indicate the relative contribution of each of these three primary colors. – If each number in the triple is given on a scale of 0 to 255, then 0 means no contribution of that color, and 255 means full contribution of that color. ICS Class 70 2.6 Representing Images and Graphics • Representing Color – Color Depth : the amount of data that is used to represent a color, it is usually expressed in terms of the number of bits that are used to represent its color. – TrueColor indicates a 24-bit color depth. Therefore, each number in an RGB value gets eight bits, which gives the range of 0 to 255 for each. This results in the ability to represent over 16.7 million unique colors. – HiColor is a term that indicates a 16-bit color depth. Five bits are used for each number in an RGB value and the extra bit is sometimes used to represent transparency. ICS Class 71 2.6 Representing Images and Graphics • Digitized Image – A photograph is an analog representation of an image. It is continuous across its surface. In order to represent and store image on a computer, we should digitize picture. – Digitizing a picture is the act of representing it as a collection of individual dots called pixels, a term that stands for picture elements. ICS Class 72 2.6 Representing Images and Graphics • Digitized Image – The number of pixels used to represent a picture is called the resolution. – If enough pixels are used (high resolution), and are then presented in the proper order side by side, the human eye can be fooled into thinking it‘s viewing a continuous picture. ICS Class 73 2.6 Representing Images and Graphics • Raster-graphics format • The storage of image information on a pixel-by-pixel basis • Several Popular Raster File Formats – BMP (bitmap) • A bitmap file contains the pixel color values of the image from left to right and top to bottom. • A bitmap file supports 24-bit TrueColor, though usually the color depth can be specified to reduce the file size. • A bitmap file may be compressed using run-length encoding. – GIF(Graphics Interchange Format) • It uses indexed color to reduce file size, which limits the number of available colors to 256. • If even fewer colors are required, the color depth can usually be specified to fewer bits. • GIF files are best used for graphics and images with few colors, and are therefore considered optimal for line art. ICS Class 74 2.6 Representing Images and Graphics • Several Popular Raster File Formats – JPEG • JPEG is a commonly used method of lossy compression for digital images • It supports different compression ratio. The degree of compression can be adjusted, allowing a selectable tradeoff between storage size and image quality. • The JPEG format is designed to exploit the nature of our eyes. Humans are more sensitive to gradual changes of brightness and color over distance than we are to rapid changes. • Therefore, the data that the JPEG format stores averages out the color hues over short distances. • A fairly complicated compression scheme is adopted to significantly reduce the file sizes. ICS Class 75 2.6 Representing Images and Graphics • Vector Representation of Graphics – A vector-graphics format describes an image in terms of lines and geometric shapes. – A vector graphic is a series of commands that describe a line‘s direction, thickness, and color. – The file sizes for vector-graphics format tend to be small . – A raster graphic must be encoded multiple times for different sizes and proportions. – Vector graphics can be resized mathematically, and these changes can be calculated dynamically as needed. ICS Class 76 2.6 Representing Images and Graphics • Vector Representation of Graphics – Vector graphics is not good for representing real-world images. JPEG images are far superior in that regard – but vector graphics is good for line art and cartoon-style drawings. ICS Class 77 Contents • 2.1 Binary Values and Number System • 2.2 Data and Computers • 2.3 Representing Numeric Data • 2.4 Representing Text • 2.5 Representing Audio Information • 2.6 Representing Image and Graphics • 2.7 Representing Video ICS Class 78 2.7 Representing Video • Video – Video clips contain the equivalent of many still images (静 态图片) – if the change between two consecutive images is small, the movement between two images seems continuous. ICS Class 79 2.7 Representing Video • Codec (COmpressor/DECompressor) – A video codec refers to the methods used to shrink the size of a movie to allow it to be played on a computer or over a network. – Almost all video codecs use lossy compression to minimize the huge amounts of data associated with video. – Video codecs employ two types of compression: temporal and spatial. ICS Class 80 2.7 Representing Video • Codec (COmpressor/DECompressor) – Temporal compression • Temporal compression looks for differences between consecutive frames. • A keyframe is chosen as the basis to compare the differences, and its entire image is stored. • For consecutive images, only the changes (called delta frames) are stored. • Temporal compression is effective in video that changes little from frame to frame, such as a scene that contains little movement. ICS Class 81 2.7 Representing Video • Codec (COmpressor/DECompressor) – Temporal compression ICS Class 82 2.7 Representing Video • Codec (COmpressor/DECompressor) – Spatial compression • Spatial compression removes redundant information within a frame. • Spatial video compression often groups pixels into blocks that have the same color, such as a portion of a clear blue sky. • Instead of storing each pixel, the color and the coordinates (坐标) of the area are stored instead. • This idea is similar to run-length. ICS Class 83 2.7 Representing Video ICS Class 84 The End ICS Class 85