Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Agenda Data Representation Numbering Systems Purpose Binary, Octal, Hexadecimal (Hex) Numbering System Conversions Binary to Octal, Octal to Binary Binary to Hex, Hex to Binary Data Representation Manipulating binary data on the computer: C Programming: Convert lowercase to uppercase. Network communication. Unix: Set access permissions for directories, files Webpage Programming: Set the colour of webpage background, text and links Before you can program at the “machine level” you need to understand how computers store data such as numbers and characters. Binary Code Each digit in the binary code (i.e “0” or “1”) is called a bit. A certain number of bits are used to represent a character or a digit (part of a large number) called a byte. How many of these binary digits (bits) should be put together to form a code (byte) to represent all the characters / numbers on a computer system? Binary Code A mathematical formula is used to determine the total number of character / number possibilities for binary code: 2^n The “2” represents the binary code (i.e. “0” and “1” in the code). The “n” represents the total number of digits in the binary code. Example: 2^8 = 256 Therefore, an eight-digit binary code would allow a total of 256 characters, or positive integers from 0 to 255, or integers from –128 to 127. Remember that zero takes up one of those numbers as well! Earlier computers used binary codes that consisted of either 7 or 8 bits. Newer computers use binary codes consisting of multiples of 8 (e.g. 8, 16, 32, 64 bit computers). Numbering Systems Humans are most comfortable with the decimal numbering system (numbers with digits 0 - 9). Early in computer development, humans were forced to communicate with (and program) computers in “binary” which was time-consuming. To compromise, humans used other numbering systems (such as octal & hexadecimal) to provide a condensed (“short-hand”) method when working with computers. Binary, octal and hexadecimal numbers are sometimes still used in programming commands as well as Unix commands. Numbering Systems Decimal (Base 10) Binary (Base 2) numbering system for computers Octal(Base 8)/ Hexadecimal(Base16) Short-hand (short-cut) method of representing binary numbers numbering system for humans Decimal Numbers (Base 10) Look at number 3,572 as sums of powers of 10: + + + = 3x10^3 5x10^2 7x10^1 2x10^0 = = = = 3x1000 5x100 7x10 2x1 = 3000 = 500 = 70 =2 3,572 Binary Numbering System You can use the same method to convert a binary number to a decimal number: Multiply digits with appropriate power of 2 (i.e. 2^0, 2^1, 2^2, 2^3, etc…) Add results together to determine decimal number. Binary Numbers (Base 2) Convert binary number 10100100 to a decimal number: + + + + + + + 1x2^7 0x2^6 1x2^5 0x2^4 0x2^3 1x2^2 0x2^1 0x2^0 = = = = = = = = 1x128 0x64 1x32 0x16 0x8 1x4 0x2 0x1 = 128 =0 = 32 =0 =0 =4 =0 =0 164 (Decimal number) Therefore the binary code 10100100 represents the decimal number 164. Octal Numbers (Base 8) Convert the octal number 264 to a decimal number: + + = 2x8^2 = 2x64 = 128 6x8^1 = 6x8 = 48 4x8^0 = 4x1 = 4 180 (Decimal number) Therefore the octal number 264 represents the decimal number 180. Hexadecimal Numbers (Base 16) Convert the Hexadecimal number A1C to a decimal number: Note - In Hex: + + = A = 10 B = 11 C = 12 D = 13 E = 14 F = 15 Ax16^2 = 10x16^2 = 10x256 = 2560 1x16^1 = 1x16^1 = 1x16 = 16 Cx16^0 = 12x16^0 = 12x1 = 12 2588 (Decimal number) Therefore the hexadecimal number A1C represents the decimal number 2588. Numbering System Shortcuts There is a relation between binary digits and other number systems like octal and hexadecimal (hex): 1 Octal digit = 3 binary digits 1 Hex digit = 4 binary digits Some Unix operations require working at the binary level. Since working at the binary level is tedious, certain Unix commands allow for the use of octal numbers to efficiently represent binary code. Binary to Octal Conversion Convert the binary number 111110000 to an octal number: = x i.e. = 1 1 1 1 1 0 0 0 0 2^2 2^1 2^0 (4) (2) (1) 1x4+ 1x2+ 1x1 2^2 2^1 2^0 (4) (2) (1) 1X4+ 1x2+ 0x1 2^2 2^1 2^0 (4) (2) (1) 0X4+ 0x2+ 0x1 7 6 0 Remember: 1 octal number is equal to 3 binary numbers. Group binary numbers into groups of 3’s starting from the right and multiply each digit by the appropriate power of 2, then add. Therefore, the binary number 111110000 represents 760 as an octal number. This code can be used to represent directory and file permissions (you will learn how to set permissions next week). Octal to Binary Conversion Similar to previous calculation, but in reverse: Convert octal number 760 to binary. 7 = 6 0 (4)(2)(1) (4)(2)(1) (4)(2)(1) 1 1 1 1 1 0 0 0 0 111110000 “Spread-out” octal number to make room for binary number result. Determine digits (0’s or 1’s) that are required when multiplied by appropriate power of 2 to add up to octal digit. Binary to Hex Conversion = Convert the binary number 111110000 to a hexadecimal number: Note: 0 0 0 1 1 1 1 1 0 0 0 0 1 hexadecimal (8) (4) (2) (1) 1 1 (8) (4) (2) (1) 15 F (8) (4) (2) (1) 0 0 number is equal to 4 binary numbers. Group binary numbers into groups of 4’s starting from the right. Add leading zeros if last group of digits is less than 4. To obtain result, multiply digit by appropriate power of 2, then add. Therefore, the binary number 111110000 represents 1F0 as a hexadecimal number. Hex to Binary Conversion Similar to previous calculation, but in reverse: Convert hexadecimal number 1F0 to binary. 1 1 (8)(4)(2)(1) 0 0 0 1 = F 15 0 0 (8)(4)(2)(1) (8)(4)(2)(1) 1 1 1 1 0 0 0 0 000111110000 = 111110000 “Spread-out” hex number to make room for binary number result. Determine digits (0’s or 1’s) that are required when multiplied by appropriate power of 2 to add up to hexadecimal digit. Hexadecimal Numbers Example When creating a webpage, you can set the background colour by using a formatting tag with a hexadecimal number. Example: <body bgcolor=“#66ff99” text=“#66cccc” link=“#0000ee”> Each 2 digit pairs indicate a colour intensity. Example of bgcolor attribute: 66 – Red colour setting ff - Blue colour setting 99 - Green colour setting The combination of red, blue and green colour settings will create any colour desired. Check the “webpage colour selector” link contained in UNX122 course notes! Representing Signed Numbers in Binary An interesting technique is used to represent signed (positive and negative) numbers: The first bit is used to represent a negative number. The remaining bits are positive. The negative (or positive number if the first bit is 0) is determined by adding sums of the power of 2. Eg. 11100101 = -(1x2^7)+1x2^6+1x2^5+0x2^4+0X2^3+1x2^2+0x2^1+1x2^0 = -27 (if recognized as a signed integer) Eg. 01100101 = -(0x2^7)+1x2^6+1x2^5+0x2^4+0X2^3+1x2^2+0x2^1+1x2^0 = +101 (if recognized as a signed integer) The computer uses special mathematical tricks such as “bit-shifting” and “two’s complement” to perform common mathematical operations other than simply add numbers.