csc8400:computersystems# - villanova computer …mdamian/past/csc8400fa15/notes/02_data... · 10...
TRANSCRIPT
2
Analog vs. Digital System q Analog Signals - Value varies con1nuously
q Digital Signals - Value limited to a finite set - Digital systems more robust
q Binary Signals - Has at most 2 values - Used to represent bit values - Bit 1me T needed to send 1 bit
Why Bits (Binary Digits)?
q Computers are built using digital circuits - Inputs and outputs can have only two values - True (high voltage) or false (low voltage) - Represented as 1 and 0
q Can represent many kinds of informa1on - Boolean (true or false) - Numbers (23, 79, …) - Characters (‘a’, ‘z’, …) - Pixels, sounds - Internet addresses
3
Coding q A single binary input can have two values: 1 or 0
q More bits = more combina1ons
0 0
0 1
1 0
1 1
Coding q How many values can you represent on 3 bits?
q What about n bits?
4
Binary Numbers
q How do you figure out what the value of 1110 is? - Same way you do for 4173, for instance
q Decimal (base 10) - Each digit represents a power of 10 - 417310 = 4 x 103 + 1 x 102 + 7 x 101 + 3 x 100
q Binary (base 2) - Each bit represents a power of 2 - 11102 = 1 x 23 + 1 x 22 + 1 x 21 + 0 x 20 = 1410
0 1 10 11 100 101 110 111 1000 1001 1010 1011 1100 1101 1110 1111 10000 10001 10010
Coun3ng in Binary 0 = 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 = 12 = 13 = 14 = 15 = 16 = 17 = 18 =
0000 0000000 000 00 00 00 00 0 0 0 0 0 0 0 0
5
Number Systems
Base Character Set 2 {0,1} 3 {0,1,2} 4 {0,1,2,3} 5 {0,1,2,3,4} 6 {0,1,2,3,4,5} 7 {0,1,2,3,4,5,6} 8 {0,1,2,3,4,5,6,7} 9 {0,1,2,3,4,5,6,7,8} 10 {0,1,2,3,4,5,6,7,8,9} 11 {0,1,2,3,4,5,6,7,8,9,A} 12 {0,1,2,3,4,5,6,7,8,9,A,B} 13 {0,1,2,3,4,5,6,7,8,9,A,B,C} 14 {0,1,2,3,4,5,6,7,8,9,A,B,C,D} 15 {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E} 16 {0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F}
Binary (Base 2) to Decimal
q Sum up (bit*weight):
28 27 26 25 24 23 22 21 20
256 128 64 32 16 8 4 2 11 0 0 0 0 0 1 1
1 0 1 0 0 1 1 0 0
100000112 = _________________________________10 ? 1010011002 = _________________________________10 ?
Binary Weights
6
Powers of 2
q Memorize!
210 29 28 27 26 25 24 23 22 21 20
1024 512 256 128 64 32 16 8 4 2 1
280 270 260 250 240 230 220 210
Yotta Zetta Exa Peta Tera Giga Mega KiloY Z E P T G M K
Octal (Base 8) to Decimal
q Sum up (digit*weight):
84 83 82 81 80
4096 512 64 8 12 5 7
1 0 2 2 6
2578 = _________________________________10 ? 102268 = _________________________________10 ?
Octal weights
7
Hexadecimal (Base 16) to Decimal
q Sum up (hex digit*weight):
163 162 161 160
4096 256 16 1A 2 B
1 4 A 6
A2B16 = _________________________________10 ? 14A616 = _________________________________10 ?
Hexadecimal Weight
0 1 2 3 4 5 6 7 10 11 12 13 14 15 16 17 20 21 22
Coun3ng in Octal 0 = 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 = 12 = 13 = 14 = 15 = 16 = 17 = 18 =
00 00 00 00 00 00 00 00 0 0 0 0 0 0 0 0 0 0 0
8
0 1 2 3 4 5 6 7 8 9 A B C D E F 10 11 12
Coun3ng in Hexadecimal 0 = 1 = 2 = 3 = 4 = 5 = 6 = 7 = 8 = 9 = 10 = 11 = 12 = 13 = 14 = 15 = 16 = 17 = 18 =
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 0 0 0
Decimal to Binary
q Use the Placement Method
1024 512 256 128 64 32 16 8 4 2 1Powers of 2
15510 = _________________________________ 2 ?
• 128 goes into 155 once leaving 27 to be placed
1 ? ? ? ? ? ? ?
• 64 and 32 are too big (make them 0)• 16 goes in once leaving 11
1 0 0 1 ? ? ? ?
• and so on
9
You Try It …
1024 512 256 128 64 32 16 8 4 2 1Powers of 2
58310 = _________________________________ 2 ?
Hexadecimal Benefits q It is often convenient to write binary (base-2) numbers
as hexadecimal (base-16) numbers instead. - fewer digits -- four bits per hex digit - less error prone -- easy to corrupt long string of 1’s and 0’s
Binary Hex Decimal 0000 0 0 0001 1 1 0010 2 2 0011 3 3 0100 4 4 0101 5 5 0110 6 6 0111 7 7
Binary Hex Decimal 1000 8 8 1001 9 9 1010 A 10 1011 B 11 1100 C 12 1101 D 13 1110 E 14 1111 F 15
10
Converting from Binary to Hexadecimal q Every four bits is a hex digit. - start grouping from right-hand side
011101010001111010011010111
7D4F8A3
This is not a new machine representation, just a convenient way to write the number.
1. Convert the hex value 0x7A8BF7D6 into its binary equivalent:
2. Convert the binary 10 0110 1110 1001 0100 1100 0101 11112
to hex:
Exercises
0111 1010 1000 1011 1111 0111 1101 0110
0010 0110 1110 1001 0100 1100 0101 1111
6 E 9 4 C 5 F 2
7 A 8 B F 7 D 6
11
3. Convert from binary to octal:
10 111 010 100 110 001 011 1112
Exercises (contd.)
010 111 010 100 110 001 011 111
7 2 4 6 1 3 7 2
Number Systems q The binary, hexadecimal (hex) and octal system share one common feature – they are all based on powers of 2.
q Each digit in the hex system is equivalent to a four-‐digit binary number and each digit in the octal system is equivalent to a 3-‐digit binary number.
12
Prac3ce
Hex Decimal Binary
10
240
11111111
Binary Addi3on q Let’s review decimal addi1on - From right to leg, we add each pair of digits - We write the sum, and add the carry to the next column
1 9 8
+ 2 6 4
Sum
Carry
2
1
6
1
4
0
13
Binary Addi3on q From right to leg, we add each pair of bits
0 + 0 = 0 0 + 1 = 1 1 + 0 = 1 1 + 1 = 10
q We write the sum, and add the carry to the next column
1 9 8
+ 2 6 4
Sum
Carry
0 1 1
+ 0 0 1
Sum
Carry 2
1
6
1
4
0
Base 10 Base 2
Data Representa3on
14
What kinds of data do we need to represent?
- Numbers – signed, unsigned, integers, floating point, complex, rational, irrational, …
- Text – characters, strings, … - Images – pixels, colors, shapes, … - Sound - Logical – true, false - Instructions - …
q Data type: - representation and operations within the computer
Word-Oriented Memory Organization
q Addresses Specify Byte Locations - Address of first byte in word - Addresses of successive
words differ by 4 (32-bit) or 8 (64-bit)
0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011
32-bitWords Bytes Addr.
0012 0013 0014 0015
64-bitWords
Addr =??
Addr =??
Addr =??
Addr =??
Addr =??
Addr =??
0000
0004
0008
0012
0000
0008
15
Data Representations q Sizes of C Objects (in Bytes)
C Data Type Sparc/Unix Typical 32-bit Intel IA32 int 4 4 4 long int 8 4 4 char 1 1 1 short 2 2 2 float 4 4 4 double 8 8 8 long double 8 8 10/12 char * 8 4 4 (or any other pointer)
Address vs. Value q Sometimes we want to deal with the address
of a memory location, rather than the value it contains.
q Adding a column of numbers. - R2 contains address of first location. - Read value, add to sum, and
increment R2 until all numbers have been processed.
q R2 is a pointer -- it contains the address of data we’re interested in.
x3107 x2819 x0110 x0310 x0100 x1110 x11B1 x0019
x3100
x3101
x3102
x3103
x3104
x3105
x3106
x3107
x3100
R2
address
value
16
Byte Ordering q How should bytes within multi-byte word be ordered in
memory?
q Conventions - Sun, Mac are “Big Endian” machines
o Least significant byte has highest address - Alpha, PC are “Little Endian” machines
o Least significant byte has lowest address
Byte Ordering Example q Big Endian - Least significant byte has highest address
q Little Endian - Least significant byte has lowest address
q Example - Variable x has 4-byte representation 0x01234567 - Address given by &x is 0x100
0x100 0x101 0x102 0x103
01 23 45 67
0x100 0x101 0x102 0x103
67 45 23 01
Big Endian
Little Endian
01 23 45 67
67 45 23 01
17
Integers
Unsigned Integers q An n-bit unsigned integer represents 2n values:
from 0 to 2n-1. 22 21 20 0 0 0 0 0 0 1 1 0 1 0 2 0 1 1 3 1 0 0 4 1 0 1 5 1 1 0 6 1 1 1 7
18
Signed Integers q How do computers differen1ate between posi1ve and nega1ve integers? - Posi1ve integers have most significant bit 0 - Nega1ve integers have most significant bit 1
q Nega1ve integer representa1ons: 1. Sign Magnitude 2. One’s Complement 3. Two’s Complement
1. Sign Magnitude q Use the legmost bit to store the sign - Zero for posi1ve number - One for nega1ve number
q Examples
0 0 1 0 1 1 0 0 è 44 1 0 1 0 1 1 0 0 è -44
q Hard to do arithme1c this way, so it is rarely used - What is the result of 44 – 44?
Sign Magnitude
19
1. Sign Magnitude (contd.)
0 0 1 0 1 1 0 0 è 44 1 0 1 0 1 1 0 0 è -44
q For numbers represented on n bits: - Range of posi1ve integers: - Range of nega1ve integers:
Sign Magnitude
from 0 to (2n-1 – 1)
from –(2n-1 – 1) to –1
2. One’s Complement q Legmost bit is 0 for posi1ve numbers
0 0 1 0 1 1 0 0 è 44
q To obtain the corresponding nega1ve number (-‐44), flip every bit:
1 1 0 1 0 0 1 1 è -44
q So -‐44 is the one’s complement of 44.
20
2. One’s Complement (contd.) q What is the result of 44 – 44?
0 0 1 0 1 1 0 0 ( 44) 1 1 0 1 0 0 1 1 (-44)
_______________
q Issue: two different representa1ons for zero
3. Two’s Complement q Legmost bit is 0 for posi1ve numbers
0 0 1 0 1 1 0 0 è 44
q To obtain the corresponding nega1ve number -‐44, add 1 to the one’s complement of 44:
1 1 0 1 0 0 1 1 è one’s complement + 0 0 0 0 0 0 0 1 _______________ 1 1 0 1 0 1 0 0 è two’s complement
21
3. Two’s Complement (contd.) q What is the result of 44 – 44?
0 0 1 0 1 1 0 0 ( 44) 1 1 0 1 0 1 0 0 (-44)
_______________
q Used by most computer systems q For numbers represented on n bits:
- Range of posi1ve integers: - Range of nega1ve integers:
from 0 to (2n-1 – 1)
from –(2n-1 – 1) to –1
Converting Two’s Complement to Decimal 1. If leading bit is one, take two’s
complement to get a positive number.
2. Add powers of 2 that have “1” in the corresponding bit positions.
3. If original number was negative, add a minus sign.
n 2n
0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512
10 1024
X = 01101000two = 26+25+23 = 64+32+8 = 104ten
Assuming 8-bit 2’s complement numbers.
22
More Examples n 2n
0 1 1 2 2 4 3 8 4 16 5 32 6 64 7 128 8 256 9 512
10 1024
Assuming 8-bit 2’s complement numbers.
X = 00100111two = 25+22+21+20 = 32+4+2+1 = 39ten
X = 11100110two -X = 00011010
= 24+23+21 = 16+8+2 = 26ten
X = -26ten
Two’s Complement Signed Integers q Most significant bit is sign bit and has weight –2n-1.
q Range of an n-bit number: -2n-1 through 2n-1 – 1. - The most negative number (-2n-1) has no positive counterpart.
-23 22 21 20 0 0 0 0 0 0 0 0 1 1 0 0 1 0 2 0 0 1 1 3 0 1 0 0 4 0 1 0 1 5 0 1 1 0 6 0 1 1 1 7
-23 22 21 20 1 0 0 0 -8 1 0 0 1 -7 1 0 1 0 -6 1 0 1 1 -5 1 1 0 0 -4 1 1 0 1 -3 1 1 1 0 -2 1 1 1 1 -1
23
Number Representa3ons Review
q Legmost bit zero indicates
q Sign Magnitude: - nega1ve values
q One’s Complement: - nega1ve values
q Two’s complement: - nega1ve values
positive number
most significant bit 1 has weight 0
are the 1s complement of positive values(flip every bit)
are the 2s complement of positive valuesmost significant bit 1 has weight –2n-1
Fill in the Table Bit
PatternValue
(Sign Magnitude)Value
(One’s Complement)Value
(Two’s Complement)
000
001
010
011
100
101
110
111
24
Ques3on q What value does 10011001 represent?
ASCII American Standard Code for
Informa3on Interchange
25
The ASCII CodeAmerican Standard Code for Information Interchange 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 0 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR SO SI
16 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US
32 SP ! " # $ % & ' ( ) * + , - . /
48 0 1 2 3 4 5 6 7 8 9 : ; < = > ?
64 @ A B C D E F G H I J K L M N O
80 P Q R S T U V W X Y Z [ \ ] ^ _
96 ` a b c d e f g h i j k l m n o
112 p q r s t u v w x y z { | } ~ DEL
Lower case: 97-122 and upper case: 65-90E.g., ‘a’ is 97 and ‘A’ is 65 (i.e., 32 apart)
char Constantsq C has char constants (sort of)
q Examples
Constant Binary Representation (assuming ASCII)
Note
'a' 01100001 letter
'0' 00110000 digit
'\x61' 01100001 hexadecimal form
Use single quotes for char constantUse double quotes for string constant
* Technically 'a' is of type int; automatically truncated to type char when appropriate
26
More char Constants
Constant Binary Representation (assuming ASCII)
Note
'\a' 00000111 alert (bell)
'\b' 00001000 backspace
'\f' 00001100 form feed
'\n' 00001010 newline
'\r' 00001101 carriage return
'\t' 00001001 horizontal tab
'\v' 00001011 vertical tab
'\\' 01011100 backslash
'\?' 00111111 question mark
'\'' 00100111 single quote
'\"' 00100010 double quote
'\0' 00000000 null
• Escape characters
Usedoften
Interesting Properties of ASCII Code q What is relationship between a decimal digit ('0', '1', …)
and its ASCII code?
q What is the difference between an upper-case letter ('A', 'B', …) and its lower-case equivalent ('a', 'b', …)?
q Given two ASCII characters, how do we tell which comes first in alphabetical order?
q Are 128 characters enough? (http://www.unicode.org/)
27
Other Data Types q Floating Points - IEEE representation, to be covered later
q Text strings - sequence of characters, terminated with NULL (0)
q Image - array of pixels
o monochrome: one bit (1/0 = black/white) o color: red, green, blue (RGB) components (e.g., 8 bits each) o other properties: transparency
q Sound - sequence of fixed-point numbers
Bit-‐Level Operators
(Common to C and Java)
29
Used in Compression
• Look at the DEFLATE algorithm, for instance - https://en.wikipedia.org/wiki/DEFLATE - everything in bits, not bytes
Overview of C bit-level operators
~ Bitwise NOT (“flips” bits) & Bitwise AND ^ Bitwise XOR | Bitwise OR
<< Bitwise left shift (shifts bits to left) >> Bitwise right shift (shifts bits to right)
30
Observation 1 – Addition q 2’s comp. addition is just binary addition. - assume all integers have the same number of bits - for now, assume that sum fits in n-bit 2’s comp. representation
01101000 (104) 11110110 (-10)+ 11110000 (-16) + (-9)
01011000 (88) (-19)
Assuming 8-bit 2’s complement numbers.
Observation 2 – Sign Extension q To add two numbers, we must represent them
with the same number of bits.
q If we just pad with zeroes on the left:
q Instead, replicate the MS bit -- the sign bit:
4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 00001100 (12, not -4; NOT GOOD!)
4-bit 8-bit 0100 (4) 00000100 (still 4) 1100 (-4) 11111100 (still -4)
31
Bit-Level Operations in C q Operations &, |, ~, ^ available in C
- Apply to any “integral” data type o long, int, short, char
- View arguments as bit vectors - Arguments applied bit-wise
q Examples (char data type) - ~0x41 --> 0xBE
~010000012 --> 101111102 - ~0x00 --> 0xFF
~000000002 --> 111111112 - 0x69 & 0x55 --> 0x41
011010012 & 010101012 --> 010000012 - 0x69 | 0x55 --> 0x7D
011010012 | 010101012 --> 011111012
Bitwise Operator: NOT (~) q NOT (~) is the same as one’s complement - Turns 0 to 1, and 1 to 0
q Examples: - Assume that x is an integer on 8 bits. Set x = 1. What is the value of ~x?
- Set the least significant bit of y to 0: y = y & _____;
0
1
~x
32
Bitwise Operators: AND (&) and OR (I) Bitwise AND (&)
Bitwise OR (|)
& 0
1
0 1 |
0
1
0 1
0 0 1 1 0 1 0 1
0 0 0 0 1 1 1 1
___
& 15
___
0 0 1 1 0 1 0 1
0 0 0 0 1 1 1 1
___
| 15
___
Assume that x is an integer variable.
q Determine the least significant bit of x: int bit = x & ________;
q Set the least significant bit of x to 1: x = x | ________;
Bitwise Operators: AND (&) and OR (I)
33
Bitwise Operator: XOR q XOR (^) - 0 if both bits are the same - 1 if the two bits are different
q For an integer x, what is the value of
x^x
^
0
1
0 1
Relations Between Operations q DeMorgan’s Laws - Express & in terms of |, and vice-versa
o A & B = ~(~A | ~B) • A and B are true if and only if neither A nor B is false
o A | B = ~(~A & ~B) • A or B are true if and only if A and B are not both false
q Exclusive-Or using Inclusive Or o A ^ B = (~A & B) | (A & ~B)
• Exactly one of A and B is true o A ^ B = (A | B) & ~(A & B)
• Either A is true, or B is true, but not both
34
Bitwise Operator: Le` Shi` q Leg shig: x << y (same as mul1ply by 2y) - Shig bit-‐vector x leg y posi1on - Fill blanks with 0
0 0 1 1 0 1 0 1 53
53<<2
Bitwise Operator: Right Shi` q Right shig: x >> y (same as divide by 2y) - Shig bit-‐vector x right y posi1ons - Fill in blanks with sign bit (this is called arithme/c shi0)
0 0 1 1 0 1 0 1 53
53>>2
1 0 1 1 0 1 0 1 -75
-75>>2
sign extension sign extension
Note: Java also has the unsigned shift operator >>> Not available in C.
35
Bitwise Operators q Print all the bits of a character from right to leg (star1ng with the least significant bit):
0 0 1 1 0 1 0 1 char c = 0xB5; void ReversePrintBits(char c) { /* add code here */ }
Bitwise Operators q Print all the bits of a character from leg to right (star1ng with the most significant bit):
0 0 1 1 0 1 0 1 char c = 0xB5; void PrintBits(char c) { /* add code here */ }
36
q Used to change or query one or more bits in a variable.
q The bitmask indicates which bits are to be affected.
q Common opera1ons: - Set one or more bits (set to 1) - Clear one or more bits (set to zero)
- Read one or more bits
q Examples: - Set bit 2 of x (bit 0 is least significant): x = x ________ - Clear bit 3 of x: x = x ____________
- Read bit 4 of x: bit = x ____________
Bitmasks
q Set the least significant byte of x to FF:
x = x ________;
q Clear the least significant byte of x:
x = x ________;
q Read the least significant byte of x:
byte = x ________;
Bitmasks
37
q Set to 1 bits 2, 4 and 7 of x (0 is least significant):
x = x ________;
q Clear bits 3, 4 and 5 of x:
x = x ________;
Bitmasks
Contrast: Logical Operators
38
Familiar Operators (common to C, Java) Category OperatorsArithmetic ++expr --expr expr++ expr--
expr1*expr2 expr1/expr2 expr1%expr2 expr1+expr2 expr1-expr2
Assignment expr1=expr2 expr1*=expr2 expr1/=expr2 expr1%=expr2 expr1+=expr2 expr1-=expr2
Relational expr1<expr2 expr1<=expr2 expr1>expr2 expr1>=expr2 expr1==expr2 expr1!=expr2
Logical !expr expr1&&expr2 expr1||expr2
Function Call func(paramlist)
Cast (type)expr
Conditional expr1?expr2:expr3
Logical Operators
q Always return 0 or 1
q View 0 as “False”
q Anything nonzero as “True”
q Examples (char data type)
- !0x41 --> _____ - !0x00 --> _____ - !!0x41 --> _____
- 0x69 && 0x55 --> _____ - 0x69 || 0x55 --> _____
&& (Logical AND), || (Logical OR), ! (Logical NOT)
39
What is the Output? int a = 0x43, b = 0x21;
printf("a | b = %x\n", a | b); _________
Printf("a || b = %x\n", a || b); _________
printf("a & b = %x\n", a & b); _________
printf("a && b = %x\n", a && b); _________
Mental Exercise /* * isNotEqual -‐ return 0 if x == y, and 1 otherwise * Examples: isNotEqual(5,5) = 0, isNotEqual(4,5) = 1 */ int isNotEqual (int x, int y) { return ______________; }
40
Limits of the Machine:
How much memory space for data?
Storage Units
1 bit = smallest unit of memory
1 byte = 8 bits
4 bytes = 1 word (system dependent)
41
Words q On most machines, bytes are assembled into larger structures called “words”, where a word is usually defined to be the number of bits the processor can operate on at one 1me.
q Some machines use four-‐byte words (32 bits), while some others use 8-‐byte words (64 bits) and some machines use less conven1onal sizes.
0000 0001 0002 0003 0004 0005 0006 0007 0008 0009 0010 0011
32-bitWords Bytes Addr.
0012 0013 0014
64-bitWords
Addr =??
Addr =??
Addr =??
Addr =??
Addr =??
Addr =??
0000
0004
0008
0012
0000
0008
The sizeof Operator
q Unique among operators: evaluated at compile-‐1me
q Evaluates to type size_t; on tanner, same as unsigned int
q Examples
Category Operators
sizeof sizeof(type) sizeof(expr)
int i = 10; double d = 100.0; … … sizeof(int) … /* On tanner, evaluates to 4 */ … sizeof(i) … /* On tanner, evaluates to 4 */ … sizeof(double)… /* On tanner, evaluates to 8 */ … sizeof(d) … /* On tanner, evaluates to 8 */ … sizeof(d + 200.0) … /* On tanner, evaluates to 8 */
42
Determining Data Sizes q To determine data sizes on your computer
q Output on tanner
#include <stdio.h> int main() { printf("char: %d\n", (int)sizeof(char)); printf("short: %d\n", (int)sizeof(short)); printf("int: %d\n", (int)sizeof(int)); printf("long: %d\n", (int)sizeof(long)); printf("float: %d\n", _________________); printf("double: %d\n", _________________); printf("long double: %d\n", _________________); return 0; }
char: 1 short: 2 int: 4 long: 4 float: 4 double: 8 long double: 16
Overflow: Running Out of Room q Adding two large integers together - Sum might be too large to store in available bits - What happens?
q We have overflow if: - signs of both operands are the same, and - sign of sum is different.
01000 (8) 11000 (-8) + 01001 (9) + 10111 (-9)
10001 (-15) 01111 (+15) Assuming 5-bit 2’s complement numbers.
43
Overflow q Unsigned integers - All arithme1c is “modulo” arithme1c - Sum would just wrap around
q Signed integers - Can get nonsense values - Example with 16-‐bit integers
o Sum: 10000+20000+30000 o Result: -‐5536
Try It Out q Write a program that computes the sum
10000+20000+30000
Use only short int variables in your code:
short int a = 10000; short int b = 20000; short int c = 20000; short int sum = a + b + c; printf("sum = %d\n", sum);
44
Exercise q Assume only four bits are available for represen1ng integers, and signed integers are represented in 2’s complement.
q Compute the value of the expression 7 + 7
short int x = 15213; unsigned short int ux = (unsigned short) x; short int y = -15213; unsigned short int uy = (unsigned short) y;
Casting Signed to Unsigned q C Allows Conversions from Signed to Unsigned
q Resulting Value - No change in bit representation - Nonnegative values unchanged
o ux = 15213 - Negative values change into (large) positive values
o uy = 50323
45
Try It Out q C code:
char a = 0xFF; unsigned char b = 0xFF; printf("a = %d\n", a); printf("b = %d\n", b);
Int to Char? Try It Out …#include <stdio.h>
int main() { char c = 0x81; int i; i = c; printf(" integer = %x\n character = %x\n", i, c); i = 0x87654321; c = i; printf(" integer = %d\n character code = %d\n", i, c); return 0; }
46
C vs. Java: Cast Conversions q Java: demo1ons are not automa1c C: demo1ons are automa1c
int i; char c; … i = c; /* Implicit promotion */ /* Sign extension in Java and C */ c = i; /* Implicit demotion */ /* Java: Compiletime error */ /* C: OK; truncation */ c = (char)i; /* Explicit demotion */ /* Truncation in Java and C */
What did we learn? q Computer represents everything in binary - Integers, floa1ng-‐point numbers, characters, … - Pixels, sounds, colors, etc.
q Memory is bytes, words, endian, two’s complement, ASCII, conver1ng between hex-‐binary-‐decimal, binary opera1ons, sign extension, limits of machine, overflow, cast conversions