1 / 18

Agenda

Agenda. Data Representation Purpose Numbering Systems 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:

olive
Download Presentation

Agenda

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Agenda • Data Representation • Purpose • Numbering Systems • Binary, Octal, Hexadecimal (Hex) • Numbering System Conversions • Binary to Octal, Octal to Binary • Binary to Hex, Hex to Binary

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

  3. 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?

  4. Binary Code • A mathematical formula is used to determine the total number of character / number possibilities for binary code: • 2^n • 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). 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 allowa total of 256 characters, or positive integers from0 to 255, or integers from –128 to 127. Remember that zero takes up one of those numbers as well!

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

  6. Numbering Systems • Decimal (Base 10) • numbering system for humans • Binary (Base 2) • numbering system for computers • Octal(Base 8)/ Hexadecimal(Base16) • Short-hand (short-cut) method of representing binary numbers

  7. Decimal Numbers (Base 10) • Look at number 3,572 as sums of powers of 10: • 3x10^3 = 3x1000 = 3000 • 5x10^2 = 5x100 = 500 • 7x10^1 = 7x10 = 70 • 2x10^0 = 2x1 = 2 • 3,572

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

  9. Binary Numbers (Base 2) • Convert binary number 10100100 to a decimal number: • 1x2^7 = 1x128 = 128 • 0x2^6 = 0x64 = 0 • 1x2^5 = 1x32 = 32 • 0x2^4 = 0x16 = 0 • 0x2^3 = 0x8 = 0 • 1x2^2 = 1x4 = 4 • 0x2^1 = 0x2 = 0 • 0x2^0 = 0x1 = 0 164 (Decimal number)Therefore the binary code 10100100 represents the decimal number 164.

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

  11. Hexadecimal Numbers (Base 16) • Convert the Hexadecimal number A1C to a decimal number: • 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. Note - In Hex: A = 10B = 11C = 12D = 13E = 14F = 15

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

  13. Binary to Octal Conversion • Convert the binary number 111110000 to an octal number: • 1 1 1 1 1 0 0 0 0 • 2^2 2^1 2^0 2^2 2^1 2^0 2^2 2^1 2^0 • i.e. (4) (2) (1) (4) (2) (1) (4) (2) (1) • 1x4+ 1x2+ 1x1 1X4+ 1x2+ 0x1 0X4+ 0x2+ 0x1 • 7 6 0 • 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). 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.

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

  15. Binary to Hex Conversion • Convert the binary number 111110000 to a hexadecimal number: • 0 0 0 1 1 1 1 1 0 0 0 0 • (8)(4) (2) (1) (8) (4) (2) (1) (8) (4) (2) (1) • 1 15 0 • 1 F 0 • Therefore, the binary number 111110000 represents 1F0 as a hexadecimal number. Note: 1 hexadecimal 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.

  16. Hex to Binary Conversion • Similar to previous calculation, but in reverse: • Convert hexadecimal number 1F0 to binary. • 1 F 0 • 1 15 0 • (8)(4)(2)(1) (8)(4)(2)(1) (8)(4)(2)(1) • 0 0 0 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.

  17. 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 settingff - Blue colour setting99 - 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!

  18. Representing SignedNumbers 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.

More Related