agenda

18
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

Upload: olive

Post on 06-Jan-2016

36 views

Category:

Documents


0 download

DESCRIPTION

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: - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 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

Page 2: Agenda

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.

Page 3: Agenda

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?

Page 4: Agenda

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!

Page 5: Agenda

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.

Page 6: Agenda

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

Page 7: Agenda

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

Page 8: Agenda

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.

Page 9: Agenda

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.

Page 10: Agenda

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.

Page 11: Agenda

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

Page 12: Agenda

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.

Page 13: Agenda

Binary to Octal Conversion Convert the binary number 111110000 to an

octal number:

= 1 1 1 1 1 0 0 0 0x 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.

Page 14: Agenda

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.

Page 15: Agenda

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.

Page 16: Agenda

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.

Page 17: Agenda

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!

Page 18: Agenda

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.