[PDF] [PDF] CS107, Lecture 2

2 Plan For Today • Bits and Bytes • Hexadecimal • Integer Representations • Unsigned Integers • Signed Integers • Casting and Combining Types 



Previous PDF Next PDF





[PDF] Unsigned and Signed Integers

25 jan 2006 · An integer is a number with no fractional part; it can be positive, negative or zero In ordinary usage, one uses a minus sign to designate a negative integer However, a computer can only store information in bits, which can only have the values zero or one



[PDF] CS107, Lecture 2

2 Plan For Today • Bits and Bytes • Hexadecimal • Integer Representations • Unsigned Integers • Signed Integers • Casting and Combining Types 



[PDF] CS107 Lecture 2

4 Lecture Plan • Bits and Bytes • Hexadecimal • Integer Representations • Unsigned Integers • Signed Integers • Overflow • Casting and Combining Types  



[PDF] Secure Coding in C and C++

In two's complement arithmetic, a signed integer ranges from -2n-1 through 2n-1- 1 ○ Unsigned integers ○ range from zero to a maximum that depends on the 



[PDF] (4-bit) unsigned integer representation modular arithmetic, overflow

x+y in n-bit unsigned arithmetic is (x + y) mod 2N in math 4-bit unsigned integers 11 + 2 13 + 5 Unsigned addition overflows if and only if a carry bit is 



[PDF] Integers

Unsigned integer types are used to store both positive and negative integers ( and zero) in a well defined range Each type, signed and unsigned, has different  



Unsafe Floating-point to Unsigned Integer Casting - ScienceDirect

For example, casting from a negative floating-point number (e g -1 0) to an unsigned integer (e g unsigned int type in C language) is unsafe/undefined, which 



[PDF] Unsigned and Signed Arithmetic MIPS Commands for Unsigned

MIPS has a separate format for unsigned and signed integers 1 Unsigned integers — are saved as 32-bit words — Example: Smallest unsigned integer is  

[PDF] unsigned integer in cpp

[PDF] unsigned keyword in cpp

[PDF] unsigned long in cpp

[PDF] unsigned short c++

[PDF] unsupervised clustering sklearn

[PDF] unsupervised learning

[PDF] unsupervised learning pdf

[PDF] unsw how to write an annotated bibliography

[PDF] unts montevideo convention

[PDF] unvalidated data in an http response header

[PDF] unwto

[PDF] uob amazon promotion

[PDF] uoh academic calendar

[PDF] uom syllabus

[PDF] uon cover page

1This document is copyright (C) Stanford Computer Science and Nick Troccoli, licensed under Creative Commons Attribution 2.5 License. All rights reserved.Based on slides created by Marty Stepp, Cynthia Lee, Chris Gregg, Lisa Yan, Jerry Cain and others.CS107 Lecture 2Bits and Bytes; Integer Representationsreading:Bryant & O'Hallaron, Ch. 2.2-2.3

2CS107 Topic 1: How can a computer represent integer numbers?

3Demo: Unexpected Behaviorcp -r /afs/ir/class/cs107/lecture-code/lect2 .

4Lecture Plan•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining Types

5Lecture Plan•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining Types

60
71

8Bits•Computers are built around the idea of two states: "on" and "off". Transistors represent this in hardware, and bits represent this in software!

9One Bit At A Time•We can combine bits, like with base-10 numbers, to represent more data. 8 bits = 1 byte.•Computer memory is just a large array of bytes! It is byte-addressable; you can't address (store location of) a bit; only a byte.•Computers still fundamentally operate on bits; we have just gotten more creative about how to represent different data as bits!•Images•Audio•Video•Text•And more...

10Base 105 9 3 4Digits 0-9 (0to base-1)

11Base 105 9 3 4onestenshundredsthousands= 5*1000 + 9*100 + 3*10 + 4*1

12Base 105 9 3 4100101102103

13Base 105 9 3 4012310X:

14Base 21 0 1 101232X:Digits 0-1 (0to base-1)

15Base 21 0 1 120212223

16Base 21 0 1 1onestwosfourseights= 1*8 + 0*4 + 1*2 + 1*1 = 1110Most significant bit (MSB)Least significant bit (LSB)

24Practice: Base 2 to Base 10What is the base-2 value 1010 in base-10?a)20b)101c)10d)5e)Other

25Practice: Base 10 to Base 2What is the base-10 value 14 in base 2?a)1111b)1110 c)1010 d)Other

26Byte Values•What is the minimum and maximum base-10 value a single byte (8 bits) can store?

27Byte Values•What is the minimum and maximum base-10 value a single byte (8 bits) can store?minimum = 0maximum = ?

28Byte Values•What is the minimum and maximum base-10 value a single byte (8 bits) can store?minimum = 0maximum = ?111111112x: 7 6 5 4 3 2 1 0

29Byte Values•What is the minimum and maximum base-10 value a single byte (8 bits) can store?minimum = 0maximum = ?•Strategy 1: 1*27+ 1*26+ 1*25+ 1*24+ 1*23+ 1*22+ 1*21+ 1*20= 255111111112x: 7 6 5 4 3 2 1 0

30Byte Values•What is the minimum and maximum base-10 value a single byte (8 bits) can store?minimum = 0maximum = 255•Strategy 1: 1*27+ 1*26+ 1*25+ 1*24+ 1*23+ 1*22+ 1*21+ 1*20= 255•Strategy 2: 28-1 = 255111111112x: 7 6 5 4 3 2 1 0

31Multiplying by Base1450 x 10 = 1450011002x 2 = 11000Key Idea: inserting 0 at the end multiplies by the base!

32Dividing by Base1450 / 10 = 14511002/ 2 = 110Key Idea: removing 0 at the end divides by the base!

33Lecture Plan•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining Types

34Hexadecimal•When working with bits, oftentimes we have large numbers with 32 or 64 bits.•Instead, we'll represent bits in base-16 instead; this is called hexadecimal.0110 1010 00110-150-150-15

35Hexadecimal•When working with bits, oftentimes we have large numbers with 32 or 64 bits.•Instead, we'll represent bits in base-16 instead; this is called hexadecimal.0-150-150-15Each is a base-16 digit!

36Hexadecimal•Hexadecimal is base-16, so we need digits for 1-15. How do we do this?0 1 2 3 4 5 6 7 8 9 a b c d e f 10 11 12 13 14 15

37HexadecimalHex digit01234567Decimal value01234567Binary value00000001001000110100010101100111Hex digit89ABCDEFDecimal value89101112131415Binary value10001001101010111100110111101111

38Hexadecimal•We distinguish hexadecimal numbers by prefixing them with 0x, and binary numbers with 0b.•E.g. 0xf5is 0b111101010x f 511110101

39Practice: Hexadecimal to BinaryWhat is 0x173Ain binary?Hexadecimal173ABinary0001011100111010

40Practice: Hexadecimal to BinaryWhat is 0b1111001010in hexadecimal? (Hint: start from the right)Binary1111001010Hexadecimal3CA

41Lecture Plan•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining Types

42Number Representations•Unsigned Integers: positive and 0 integers. (e.g. 0, 1, 2, ... 99999...•Signed Integers:negative, positive and 0 integers. (e.g. ...-2, -1, 0, 1,... 9999...)•Floating Point Numbers:real numbers. (e,g. 0.1, -12.2, 1.5x1012)

43Number Representations•Unsigned Integers: positive and 0 integers. (e.g. 0, 1, 2, ... 99999...•Signed Integers:negative, positive and 0 integers. (e.g. ...-2, -1, 0, 1,... 9999...)•Floating Point Numbers:real numbers. (e,g. 0.1, -12.2, 1.5x1012)Look up IEEE floating point if you're interested!

44Number RepresentationsC DeclarationSize (Bytes)int4double8float4char1char *8short2long8

45In The Days Of Yore...C DeclarationSize (Bytes)int4double8float4char1char *4short2long4

46Transitioning To Larger Datatypes•Early 2000s:most computers were 32-bit. This means that pointers were 4 bytes(32 bits).•32-bit pointers store a memory address from 0 to 232-1, equaling 232bytes of addressable memory. This equals 4 Gigabytes, meaning that 32-bit computers could have at most 4GBof memory (RAM)!•Because of this, computers transitioned to 64-bit.This means that datatypes were enlarged; pointers in programs were now 64 bits.•64-bit pointers store a memory address from 0 to 264-1, equaling 264bytes of addressable memory.This equals 16 Exabytes, meaning that 64-bit computers could have at most 1024*1024*1024 GBof memory (RAM)!

47Lecture Plan•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining Types

48Unsigned Integers•An unsignedinteger is 0 or a positive integer (no negatives).•We have already discussed converting between decimal and binary, which is a nice 1:1 relationship. Examples:0b0001 = 10b0101 = 50b1011 = 110b1111 = 15•The range of an unsigned number is 0 → 2w-1, where wis the number of bits. E.g. a 32-bit integer can represent 0 to 232-1 (4,294,967,295).

49Unsigned Integers

50Let's Take A BreakTo ponder during the break:A signedinteger is a negative, 0, or positive integer. How can we represent both negative andpositive numbers in binary?

51Lecture Plan•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining Types

52Signed Integers•A signedinteger is a negative integer, 0, or a positive integer.•Problem:How can we represent negative andpositive numbers in binary?

53Signed Integers•A signedinteger is a negative integer, 0, or a positive integer.•Problem:How can we represent negative andpositive numbers in binary?Idea: let's reserve the most significant bit to store the sign.

54Sign Magnitude Representation0110positive61011negative3

55Sign Magnitude Representation0000positive01000negative0í ¾

56Sign Magnitude Representation•We've only represented 15 of our 16 available numbers!1 000 = -01 001 = -11 010 = -21 011 = -31 100 = -41 101 = -51 110 = -61 111 = -70 000 = 00001 = 10010 = 20011 = 30100 = 40101 = 50110 = 60111 = 7

57Sign Magnitude Representation•Pro: easy to represent, and easy to convert to/from decimal.•Con: +-0 is not intuitive•Con:we lose a bit that could be used to store more numbers•Con:arithmetic is tricky: we need to find the sign, then maybe subtract (borrow and carry, etc.), then maybe change the sign. This complicates the hardware support for something as fundamental as addition.Can we do better?

58A Better Idea•Ideally, binary addition would just workregardlessof whether the number is positive or negative.0101????0000+

59A Better Idea•Ideally, binary addition would just workregardlessof whether the number is positive or negative.010110110000+

60A Better Idea•Ideally, binary addition would just workregardlessof whether the number is positive or negative.0011????0000+

61A Better Idea•Ideally, binary addition would just workregardlessof whether the number is positive or negative.001111010000+

62A Better Idea•Ideally, binary addition would just workregardlessof whether the number is positive or negative.0000????0000+

63A Better Idea•Ideally, binary addition would just workregardlessof whether the number is positive or negative.000000000000+

64A Better IdeaDecimalPositiveNegative000000000100011111200101110300111101401001100501011011601101010701111001DecimalPositiveNegative81000100091001 (same as -7!)NA101010 (same as -6!)NA111011 (same as -5!)NA121100 (same as -4!)NA131101 (same as -3!)NA141110 (same as -2!)NA151111 (same as -1!)NA

65There Seems Like a Pattern Here...010110110000+001111010000+000000000000+•The negative number is the positive number inverted, plus one!

66There Seems Like a Pattern Here...A binary number plus its inverse is all 1s.Add 1 to this to carry over all 1s and get 0!010110101111+111100010000+

67Another Trick•To find the negative equivalent of a number, work right-to-left and write down all digits through when you reach a 1. Then, invert the rest of the digits.100100??????000000+

68Another Trick•To find the negative equivalent of a number, work right-to-left and write down all digits through when you reach a 1. Then, invert the rest of the digits.100100???100000000+

69Another Trick•To find the negative equivalent of a number, work right-to-left and write down all digits through when you reach a 1. Then, invert the rest of the digits.100100011100000000+

70Two's Complement

71Two's Complement•In two's complement, we represent a positive number as itself, and its negative equivalent as the two's complement of itself.•The two's complementof a number is the binary digits inverted, plus 1.•This works to convert from positive to negative, andback from negative to positive!

72Two's Complement•Con:more difficult to represent, and difficult to convert to/from decimal and between positive and negative.•Pro:only 1 representation for 0!•Pro: all bits are used to represent as many numbers as possible•Pro: the most significant bit still indicates the sign of a number.•Pro: addition works for any combination of positive and negative!

73Two's Complement•Adding two numbers is just...adding! There is no special case needed for negatives. E.g. what is 2 + -5?001010111101+2-5-3

74Two's Complement•Subtracting two numbers is just performing the two's complement on one of them and then adding. E.g. 4 -5 = -1.01000101-45010010111111+4-5-1

75Practice: Two's ComplementWhat are the negative or positive equivalents of the numbers below?a)-4 (1100)b)7 (0111)c)3 (0011)d)-8 (1000)

76Practice: Two's ComplementWhat are the negative or positive equivalents of the numbers below?a)-4 (1100)b)7 (0111)c)3 (0011)

77Lecture Plan•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining Types

78Overflow•If you exceed the maximumvalue of your bit representation, you wrap around or overflowback to the smallestbit representation.0b1111 + 0b1 = 0b0000•If you go below the minimumvalue of your bit representation, you wrap around or overflowback to the largestbit representation.0b0000 -0b1 = 0b1111

79Min and Max Integer ValuesTypeSize (Bytes)MinimumMaximumchar1-128127unsigned char10255short2-3276832767unsigned short2065535int4-21474836482147483647unsigned int404294967295long8-92233720368547758089223372036854775807unsigned long8018446744073709551615

80Min and Max Integer ValuesINT_MIN, INT_MAX, UINT_MAX, LONG_MIN, LONG_MAX, ULONG_MAX, ...

82OverflowAt which points can overflow occur for signed and unsigned int? (assume binary values shown are all 32 bits)A.Signed and unsigned can both overflow at points X and YB.Signed can overflow only at X, unsigned only at YC.Signed can overflow only at Y, unsigned only at XD.Signed can overflow at X and Y, unsigned only at XE.OtherXY000...000111...111011...111100...000000...001000...010000...011111...110111...101111...100100...001100...010011...110011...101......

83Unsigned Integers000...000111...111011...111100...000000...001000...010000...011111...110111...101111...100100...001100...010011...110011...101......0≈+4billionDiscontinuity means overflow possible hereIncreasing positivenumbersMore increasing positivenumbers

84Signed Numbers000...000111...111011...111100...000000...001000...010000...011111...110111...101111...100100...001100...010011...110011...101......0-1Discontinuity means overflow possible hereIncreasing positive numbersNegative numbersbecoming less negative (i.e. increasing)≈+2billion≈-2billion+1

85Overflow In Practice: PSYYouTube: "We never thought a video would be watched in numbers greater than a 32-bit integer (=2,147,483,647 views), but that was before we met PSY. "Gangnam Style" has been viewed so many times we had to upgrade to a 64-bit integer (9,223,372,036,854,775,808)!"

86Overflow In Practice: Timestamps•Many systems store timestamps as the number of seconds since Jan. 1, 1970in a signed 32-bit integer.•Problem:the latest timestamp that can be represented this way is 3:14:07 UTC on Jan. 13 2038!

87Overflow In Practice: Gandhi•In the game "Civilization", each civilization leader had an "aggression" rating. Gandhi was meant to be peaceful, and had a score of 1.•If you adopted "democracy", all players' aggression reduced by 2. Gandhi's went from 1 to 255!•Gandhi then became a big fan of nuclear weapons.https://kotaku.com/why-gandhi-is-such-an-asshole-in-civilization-1653818245

88Overflow in Practice:•Pacman Level 256•Make sure to reboot Boeing Dreamlinersevery 248 days•Comair/Delta airline had to cancel thousands of flights days before Christmas•Reported vulnerability CVE-2019-3857 in libssh2 may allow a hacker to remotely execute code•Donkey Kong Kill Screen

89Demo Revisited: Unexpected Behaviorairline.c

90Lecture Plan•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining Types

91printfand Integers•There are 3 placeholders for 32-bit integers that we can use:•%d: signed 32-bit int•%u: unsigned 32-bit int•%x: hex 32-bit int•The placeholder - not the expression filling in the placeholder - dictates what gets printed!

92Casting•What happens at the byte level when we cast between variable types? The bytes remain the same!This means they may be interpreted differently depending on the type.intv = -12345;unsigned intuv= v;printf("v = %d, uv= %u\n", v, uv);This prints out: "v = -12345, uv= 4294954951". Why?

93Casting•What happens at the byte level when we cast between variable types? The bytes remain the same!This means they may be interpreted differently depending on the type.intv = -12345;unsigned intuv= v;printf("v = %d, uv= %u\n", v, uv);The bit representation for -12345 is 0b11111111111111111100111111000111.If we treat this binary representation as a positive number, it's huge!

94Casting

95Comparisons Between Different Types•Be carefulwhen comparing signed and unsigned integers. C will implicitly castthe signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.ExpressionTypeEvaluationCorrect?0 == 0U-1 < 0-1 < 0U2147483647 > -2147483647 -12147483647U > -2147483647 -12147483647 > (int)2147483648U-1 > -2(unsigned)-1 > -2

96Comparisons Between Different Types•Be carefulwhen comparing signed and unsigned integers. C will implicitly castthe signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.ExpressionTypeEvaluationCorrect?0 == 0UUnsigned1yes-1 < 0-1 < 0U2147483647 > -2147483647 -12147483647U > -2147483647 -12147483647 > (int)2147483648U-1 > -2(unsigned)-1 > -2

97Comparisons Between Different Types•Be carefulwhen comparing signed and unsigned integers. C will implicitly castthe signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.ExpressionTypeEvaluationCorrect?0 == 0UUnsigned1yes-1 < 0Signed1yes-1 < 0U2147483647 > -2147483647 -12147483647U > -2147483647 -12147483647 > (int)2147483648U-1 > -2(unsigned)-1 > -2

98Comparisons Between Different Types•Be carefulwhen comparing signed and unsigned integers. C will implicitly castthe signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.ExpressionTypeEvaluationCorrect?0 == 0UUnsigned1yes-1 < 0Signed1yes-1 < 0UUnsigned0No!2147483647 > -2147483647 -12147483647U > -2147483647 -12147483647 > (int)2147483648U-1 > -2(unsigned)-1 > -2

99Comparisons Between Different Types•Be carefulwhen comparing signed and unsigned integers. C will implicitly castthe signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.ExpressionTypeEvaluationCorrect?0 == 0UUnsigned1yes-1 < 0Signed1yes-1 < 0UUnsigned0No!2147483647 > -2147483647 -1Signed1yes2147483647U > -2147483647 -12147483647 > (int)2147483648U-1 > -2(unsigned)-1 > -2

100Comparisons Between Different Types•Be carefulwhen comparing signed and unsigned integers. C will implicitly castthe signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.ExpressionTypeEvaluationCorrect?0 == 0UUnsigned1yes-1 < 0Signed1yes-1 < 0UUnsigned0No!2147483647 > -2147483647 -1Signed1yes2147483647U > -2147483647 -1Unsigned0No!2147483647 > (int)2147483648U-1 > -2(unsigned)-1 > -2

101Comparisons Between Different Types•Be carefulwhen comparing signed and unsigned integers. C will implicitly castthe signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.ExpressionTypeEvaluationCorrect?0 == 0UUnsigned1yes-1 < 0Signed1yes-1 < 0UUnsigned0No!2147483647 > -2147483647 -1Signed1yes2147483647U > -2147483647 -1Unsigned0No!2147483647 > (int)2147483648USigned1No!-1 > -2(unsigned)-1 > -2

102Comparisons Between Different Types•Be carefulwhen comparing signed and unsigned integers. C will implicitly castthe signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.ExpressionTypeEvaluationCorrect?0 == 0UUnsigned1yes-1 < 0Signed1yes-1 < 0UUnsigned0No!2147483647 > -2147483647 -1Signed1yes2147483647U > -2147483647 -1Unsigned0No!2147483647 > (int)2147483648USigned1No!-1 > -2Signed1yes(unsigned)-1 > -2

103Comparisons Between Different Types•Be carefulwhen comparing signed and unsigned integers. C will implicitly castthe signed argument to unsigned, and then performs the operation assuming both numbers are non-negative.ExpressionTypeEvaluationCorrect?0 == 0UUnsigned1yes-1 < 0Signed1yes-1 < 0UUnsigned0No!2147483647 > -2147483647 -1Signed1yes2147483647U > -2147483647 -1Unsigned0No!2147483647 > (int)2147483648USigned1No!-1 > -2Signed1yes(unsigned)-1 > -2Unsigned1yes

104Comparisons Between Different TypesWhich many of the following statements are true?(assume that variables are set to values that place them in the spots shown) s3 > u3u2 > u4s2 > s4s1 > s2u1 > u2s1 > u3

105Comparisons Between Different TypesWhich many of the following statements are true?(assume that variables are set to values that place them in the spots shown) s3 > u3-trueu2 > u4s2 > s4s1 > s2u1 > u2s1 > u3

106Comparisons Between Different TypesWhich many of the following statements are true?(assume that variables are set to values that place them in the spots shown) s3 > u3-trueu2 > u4-trues2 > s4s1 > s2u1 > u2s1 > u3

107Comparisons Between Different TypesWhich many of the following statements are true?(assume that variables are set to values that place them in the spots shown) s3 > u3-trueu2 > u4-trues2 > s4-falses1 > s2u1 > u2s1 > u3

108Comparisons Between Different TypesWhich many of the following statements are true?(assume that variables are set to values that place them in the spots shown) s3 > u3-trueu2 > u4-trues2 > s4-falses1 > s2-trueu1 > u2s1 > u3

109Comparisons Between Different TypesWhich many of the following statements are true?(assume that variables are set to values that place them in the spots shown) s3 > u3-trueu2 > u4-trues2 > s4-falses1 > s2-trueu1 > u2-trues1 > u3

110Comparisons Between Different TypesWhich many of the following statements are true?(assume that variables are set to values that place them in the spots shown) s3 > u3-trueu2 > u4-trues2 > s4-falses1 > s2-trueu1 > u2-trues1 > u3 -true

111Expanding Bit Representations•Sometimes, we want to convert between two integers of different sizes (e.g. shortto int, or intto long).•We might not be able to convert from a bigger data type to a smaller data type, but we do want to always be able to convert from a smallerdata type to a biggerdata type.•For unsignedvalues, we can add leading zerosto the representation("zero extension")•For signed values, we can repeat the sign of the value for new digits ("sign extension"•Note: when doing <, >, <=, >= comparison between different size types, it will promote to the larger type.

112Expanding Bit Representationunsigned short s = 4; // short is a 16-bit format, so s = 0000 0000 0000 0100bunsigned inti= s;// conversion to 32-bit int, so i= 0000 0000 0000 0000 0000 0000 0000 0100b

113Expanding Bit Representationshort s = 4; // short is a 16-bit format, so s = 0000 0000 0000 0100binti= s;// conversion to 32-bit int, so i= 0000 0000 0000 0000 0000 0000 0000 0100b - or - short s = -4; // short is a 16-bit format, so s = 1111 1111 1111 1100binti= s;// conversion to 32-bit int, so i= 1111 1111 1111 1111 1111 1111 1111 1100b

114Truncating Bit RepresentationIf we want to reducethe bit size of a number, C truncatesthe representation and discards the more significant bits.What happens here? Let's look at the bits in x (a 32-bit int), 53191:0000 0000 0000 0000 1100 1111 1100 0111When we cast x to a short, it only has 16-bits, and C truncates the number:1100 1111 1100 0111This is -12345! And when we cast sxback an int, we sign-extend the number.1111 1111 1111 1111 1100 1111 1100 0111// still -12345intx = 53191;short sx= x;inty = sx;

115Truncating Bit RepresentationIf we want to reducethe bit size of a number, C truncatesthe representation and discards the more significant bits.What happens here? Let's look at the bits in x (a 32-bit int), -3:1111 1111 1111 1111 1111 1111 1111 1101When we cast x to a short, it only has 16-bits, and C truncates the number:1111 1111 1111 1101This is -3! If the number does fit, it will convert fine.y looks like this:1111 1111 1111 1111 1111 1111 1111 1101// still -3intx = -3;short sx= x;inty = sx;

116Truncating Bit RepresentationIf we want to reducethe bit size of a number, C truncatesthe representation and discards the more significant bits.What happens here? Let's look at the bits in x (a 32-bit unsigned int), 128000:0000 0000 0000 0001 1111 0100 0000 0000When we cast x to a short, it only has 16-bits, and C truncates the number:1111 0100 0000 0000This is 62464! Unsigned numbers can lose info too.Here is what y looks like:0000 0000 0000 0000 1111 0100 0000 0000// still 62464unsigned intx = 128000;unsigned short sx= x;unsigned inty = sx;

117The sizeofOperatorlong sizeof(type);// Examplelong int_size_bytes= sizeof(int);// 4long short_size_bytes= sizeof(short); // 2long char_size_bytes= sizeof(char); // 1sizeoftakes a variable type as a parameter and returns the size of that type, in bytes.

118Recap•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining TypesNext time: How can we manipulate individual bits and bytes?

119Recap•Bits and Bytes•Hexadecimal•Integer Representations•Unsigned Integers•Signed Integers•Overflow•Casting and Combining TypesNext time: How can we manipulate individual bits and bytes?

120Additional Live Session Slides

121Notes•A literal in your C program is treated by default as a signed int(32 bits)•Wewritehex / binary only up to the most significant 1•E.g.wecouldwriteashortas0b1101-thereare12 leadingzeros,butweomitthem•Ifwehaveanegativenumber xin binary, howdowefindwhatitis in base-10?1.Find thetwo's complement of the number, y(y = -x)2.Figure out what y isinbase-103.Then we solve for x: x = -y

122Practice: Two's ComplementWhat are the negative or positive equivalents of the 8-bit numbers below?a)-4 (0b11111100)b)24 (0b11000)c)36 (0b100100)d)-17 (0b11101111)

123Practice: Two's ComplementWhat are the negative or positive equivalents of the 8-bit numbers below?a)-4 (0b11111100) -> 0b100b)24 (0b11000) -> 0b11101000c)36 (0b100100) -> 0b11011100d)-17 (0b11101111) -> 0b10001

124Practice: TruncationWhat are the values of cx for the passages of code below?shortx = 130;// 0b1000 0010charcx = x;shortx = -132 // 0b1111 1111 0111 1100charcx = x;shortx = 25;// 0b1 1001charcx = x;

125Practice: TruncationWhat are the values of cx for the passages of code below?shortx = 130;// 0b1000 0010charcx = x;// -126shortx = -132 // 0b1111 1111 0111 1100charcx = x;// 124shortx = 25;// 0b1 1001charcx = x;// 25

126Practice: TruncationWhat are the values of cx for the passages of code below?shortx = 390;// 0b1 1000 0110charcx = x;shortx = -15;// 0b1111 1111 1111 0001charcx = x;

127Practice: TruncationWhat are the values of cx for the passages of code below?shortx = 390;// 0b1 1000 0110charcx = x;// -122shortx = -15;// 0b1111 1111 1111 0001charcx = x;// -15

128Practice: ComparisonsWhat are the results of the char comparisons performed below?1.-7 < 42.-7 < 4U3.(char)130 > 44.(char)-132> 2

129Practice: ComparisonsWhat are the results of the char comparisons performed below?1.-7 < 4 -true2.-7 < 4U -false3.(char)130 > 4 -false4.(char)-132 > 2 -true

quotesdbs_dbs9.pdfusesText_15