Presentation is loading. Please wait.

Presentation is loading. Please wait.

Topic: Binary Encoding – Part 2

Similar presentations


Presentation on theme: "Topic: Binary Encoding – Part 2"— Presentation transcript:

1 Topic: Binary Encoding – Part 2
CMPT 120 Topic: Binary Encoding – Part 2

2 Decimal numeral system
Last Lecture Binary Encoding How characters are represented in computer memory How numbers are represented in computer memory But before to do this, we need to introduce decimal and binary numeral systems Counting and adding numbers in these numeral systems Converting binary numbers <-> decimal numbers Decimal numeral system Binary numeral system

3 Learning Outcomes At the end of this course, a student is expected to:
Manipulate binary encodings of simple data types. Do conversions from binary system to decimal and vice versa Determine the range of representable values within binary system Do additions and incremental counting within binary system Decode/encode a bit string to/from its corresponding value (unsigned, 2's complement, ASCII)

4 Today’s Menu Binary encoding
How numbers integers are represented in computer memory Unsigned integers Signed integers Sign and magnitude 1’s complement 2's complement How many values can they represent -> range of values

5 How integers are represented in computer memory?
Representing an integer in computer memory is often called fixed point representation (or notation) Size of computer memory location in which an integer is stored is a factor of 8 bits (1 byte) For example: 8-bit fixed point representation -> 16-bit “ “ “ > 32-bit “ “ “ > 64-bit “ “ “ > Two types of integers: unsigned and signed

6 Unsigned versus signed integer
Unsigned integer Signed integer

7 How unsigned integers are represented in computer memory?
unsigned integers are stored in computer memory locations of x-bit size where x can be 8, 16, 32, 64 Example:

8 Range of values of an unsigned integer
If the computer memory location containing an unsigned integer is of size x-bit where x = 8, how many different values can this unsigned integer have?

9 In general… The range of an unsigned integer is [0 .. 2x-1]
If x = > [ ] = [ ] If x = 16 -> [ ] = [ ] If x = 32 -> [ ] = [ ] If x = 64 -> [ ] = [ ] An unsigned integer can have 2x different values If x = > 28 = 256 If x = 16 -> 216 = If x = 32 -> 232 = If x = 64 -> 264 = Note that, aside from 0, all of these values are positive

10 How positive signed integers are represented in computer memory?
positive signed integers are stored in computer memory locations of x-bit size where x can be 8, 16, 32, 64 Example:

11 How negative signed integers are represented in computer memory?
are also stored in computer memory locations of x-bit size where x can be 8, 16, 32, 64 are represented using the following conventions: Sign and magnitude 1’s complement 2’s complement All conventions: sign represented by leftmost bit 0 for positive integer 1 for negative integer

12 A. Sign and magnitude Let’s store the integer _______ in an 8-bit memory location using the sign and magnitude convention: Convert the absolute value of the integer to a binary # Pad the resulting binary number to fill a size of 8-bit Set the sign bit (leftmost bit) to appropriate value -> 1

13 B. 1’s Complement Let’s store the integer _______ in an 8-bit memory location using the 1’s complement convention: Convert the absolute value of the integer to a binary # Pad the resulting binary number to fill a size of 8-bit Flip all the bits!

14 C. 2’s Complement Let’s store the integer _______ in an 8-bit memory location using the 2’s complement convention: Convert the absolute value of the integer to a binary # Pad the resulting binary number to fill a size of 8-bit Flip all the bits! Then, add 12.

15 Which convention does the computer use?
2’s complement Why? To understand why, let’s have a look at the table on the next slide!

16 x = 8 Decimal 2’s complement 1’s complement Sign and magnitude +7
+6 +5 +4 +3 +2 +1 +0 -0 -1 -2 -3 -4 -5 -6 -7 -8 x = 8

17 Converting back To convert a 2’s complement negative signed integer back to its decimal equivalent: Flip all the bits! Then, add 12. Convert this binary number back to a signed integer

18 Range of value of a signed integer
The range of a signed integer is [-2x x-1-1] If x = 8, then the range a signed integer is [ ] = [ ] = [ ] A signed integer can also have 2x different values If x = 8, then a signed integer can also have 28 = 256 different values, i.e., any of the value in the range -128 to 127 Note that, aside from 0, half of these values are negative and the other half (-1) are positive

19 In general… The range of an signed integer is [-2x-1 .. 2x-1-1]
If x = > [ ] = [ ] = [ ] If x = 16 -> [ ] If x = 32 -> [ ] If x = 64 -> [ ] An signed integer can have 2x different values If x = > 28 = 256 If x = 16 -> 216 = If x = 32 -> 232 = If x = 64 -> 264 = Note that, aside from 0, half of these values are negative and the other half (-1) are positive

20 When to use a unsigned integer as opposed to a signed integer
Imagine we are developing a database application We represent the number of elements in our database using a variable elementCount What would the data type of this variable be? In other words, how would we represent this variable in the computer memory? Let’s assume 16-bit memory location Impact of choosing data type signed integer Range of values: [ to 32767] Since this variable will only be assigned either 0 or a positive value (we cannot have a negative number representing the number of elements in our database), only half of the range of the signed integer, namely 0 to (32768 values) is available to us Impact of choosing the data type unsigned integer Range of values: [0 to 65535] Since this variable will only be assigned either 0 or a positive value (we cannot have a negative number representing the number of elements in our database), the range of values available to us is different values Hence, twice has many values as if we choose to represent this variable (in the computer memory) using a signed integer 

21 Summary Binary encoding 01101000101001010010011011110010
How integers are represented in computer memory Unsigned integers Signed integers Sign and magnitude 1’s complement 2's complement How many values can they represent -> range of values The meaning of a sequence of 0’s and 1’s in x bytes in memory, such as: is determined by the context, as it could be Unicode character, an integer, a floating point number, a colour, a sound, a microprocessor (CPU) instruction, etc …

22 Next Lecture Searching algorithms


Download ppt "Topic: Binary Encoding – Part 2"

Similar presentations


Ads by Google