1 / 35

Integral Data Types in C

Integral Data Types in C. First lecture response sent out Lots of good questions Some questions will be punted to reading when appropriate 3 statements useful – found a few misunderstandings Need to focus on statements, not just nouns Still need pictures Gedankenexperiment

ivi ivi
Download Presentation

Integral Data Types in C

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. Integral Data Types in C

  2. First lecture response sent out • Lots of good questions • Some questions will be punted to reading when appropriate • 3 statements useful – found a few misunderstandings • Need to focus on statements, not just nouns • Still need pictures • Gedankenexperiment • What happens when you crank a stereo system to 11 (and beyond)?

  3. Goals for this Lecture • Help you to learn about: • The binary number system • Why binary? • Converting between decimal and binary • The octal and hexadecimal number systems • Finite representations of binary integers • Unsigned integers • Integer addition • Signed integers • Bitwise operators • And, or, not, and xor • Shift-left and shift-right • The C integral data types • char, short, int, long (signed and unsigned)

  4. Why Bits (Binary Digits)? • 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 • Can represent many kinds of information • Boolean (true or false) • Numbers (23, 79, …) • Characters (‘a’, ‘z’, …) • Pixels • Sound • Can manipulate in many ways • Read and write • Logical operations • Arithmetic • …

  5. Base 10 and Base 2 • Decimal (base 10) • Each digit represents a power of 10 • 4173 = 4 x 103 + 1 x 102 + 7 x 101 + 3 x 100 • Binary (base 2) • Each bit represents a power of 2 • 10110 = 1 x 24 + 0 x 23 + 1 x 22 + 1 x 21 + 0 x 20 = 22 Decimal to binary conversion: Divide repeatedly by 2 and keep remainders 12/2 = 6 R = 0 6/2 = 3 R = 0 3/2 = 1 R = 1 1/2 = 0 R = 1 Result = 1100

  6. Writing Bits is Tedious for People • Octal (base 8) • Digits 0, 1, …, 7 • Hexadecimal (base 16) • Digits 0, 1, …, 9, A, B, C, D, E, F Thus the 16-bit binary number 1011 0010 1010 1001 converted to hex is B2A9

  7. Representing Colors: RGB • Three primary colors • Red • Green • Blue • Strength • 8-bit number for each color (e.g., two hex digits) • So, 24 bits to specify a color • In HTML, on the course Web page • Red: <font color="#FF0000"><i>Symbol Table Assignment Due</i> • Blue: <font color="#0000FF"><i>Fall Recess</i></font> • Same thing in digital cameras • Each pixel is a mixture of red, green, and blue

  8. Finite Representation of Integers • Fixed number of bits in memory • Usually 8, 16, or 32 bits • Unsigned integer • No sign bit • Always positive or 0 • All arithmetic is modulo 2n • Examples of unsigned integers • 00000001  1 • 00001111  15 • 00010000  16 • 00100001  33 • 11111111  255

  9. Adding Two Integers: Base 10 • From right to left, we add each pair of digits • We write the sum, and add the carry to the next column 0 1 1 + 0 0 1 Sum Carry 1 9 8 + 2 6 4 Sum Carry 4 0 6 1 2 1 1 0 0 1 0 1

  10. Binary Sums and Carries a b Sum a b Carry 0 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 0 0 1 1 0 1 1 1 AND XOR 69 0100 0101 103 + 0110 0111 172 1010 1100

  11. Modulo Arithmetic • Consider only numbers in a range • E.g., five-digit car odometer: 0, 1, …, 99999 • E.g., eight-bit numbers 0, 1, …, 255 • Roll-over when you run out of space • E.g., car odometer goes from 99999 to 0, 1, … • E.g., eight-bit number goes from 255 to 0, 1, … • Adding 2n doesn’t change the answer • For eight-bit number, n=8 and 2n=256 • E.g., (37 + 256) mod 256 is simply 37 • This can help us do subtraction… • Suppose you want to compute a – b • Note that this equals a + (256 -1 - b) + 1

  12. One’s and Two’s Complement • One’s complement: flip every bit • E.g., b is 01000101 (i.e., 69 in decimal) • One’s complement is 10111010 • That’s simply 255-69 • Subtracting from 11111111 is easy (no carry needed!) • Two’s complement • Add 1 to the one’s complement • E.g., (255 – 69) + 1  1011 1011 1111 1111 b - 0100 0101 one’s complement 1011 1010

  13. Putting it All Together • Computing “a – b” • Same as “a + 256 – b” • Same as “a + (255 – b) + 1” • Same as “a + onesComplement(b) + 1” • Same as “a + twosComplement(b)” • Example: 172 – 69 • The original number 69: 0100 0101 • One’s complement of 69: 1011 1010 • Two’s complement of 69: 1011 1011 • Add to the number 172: 1010 1100 • The sum comes to: 0110 0111 • Equals: 103 in decimal 1010 1100 + 1011 1011 1 0110 0111

  14. Signed Integers • Sign-magnitude representation • Use one bit to store the sign • Zero for positive number • One for negative number • Examples • E.g., 0010 1100  44 • E.g., 1010 1100  -44 • Hard to do arithmetic this way, so it is rarely used • Complement representation • One’s complement • Flip every bit • E.g., 1101 0011  -44 • Two’s complement • Flip every bit, then add 1 • E.g., 1101 0100  -44

  15. Overflow: Running Out of Room • Adding two large integers together • Sum might be too large to store in the number of bits available • What happens? • Unsigned integers • All arithmetic is “modulo” arithmetic • Sum would just wrap around • Signed integers • Can get nonsense values • Example with 16-bit integers • Sum: 10000+20000+30000 • Result: -5536

  16. Bitwise AND (&) Mod on the cheap! E.g., h = 53 & 15; Bitwise OR (|) | & 0 0 1 1 0 0 0 0 0 1 1 1 0 1 1 1 Bitwise Operators: AND and OR 53 0 0 1 1 0 1 0 1 & 15 0 0 0 0 1 1 1 1 5 0 0 0 0 0 1 0 1

  17. Bitwise Operators: Not and XOR • One’s complement (~) • Turns 0 to 1, and 1 to 0 • E.g., set last three bits to 0 • x = x & ~7; • XOR (^) • 0 if both bits are the same • 1 if the two bits are different ^ 0 1 0 0 1 1 1 0

  18. Bitwise Operators: Shift Left/Right • Shift left (<<): Multiply by powers of 2 • Shift some # of bits to the left, filling the blanks with 0 • Shift right (>>): Divide by powers of 2 • Shift some # of bits to the right • For unsigned integer, fill in blanks with 0 • What about signed negative integers? Varies across machines… • Can vary from one machine to another! 53 0 0 1 1 0 1 0 0 53<<2 1 1 0 1 0 0 0 0 53 0 0 1 1 0 1 0 0 53>>2 0 0 0 0 1 1 0 1

  19. Data Types • Programming languages combine: • Bits into bytes • Bytes into larger entities • Combinations of bytes have types; why? • Facilitates abstraction • Enables compiler to do type checking • C has 11 primitive data types • 8 integral data types (described in this lecture) • 3 floating-point data types (described in next lecture)

  20. Integral types: * On hats; C90 standard does not specify size C Integral Data Types

  21. C Integral Data Types (cont.) • Why char vs. short vs. int vs. long? • Small sizes conserve memory • Large sizes provide more range • Why signed vs. unsigned? • Signed types allow negatives • Unsigned types allow greater positives • (Dubious value: Java omits unsigned types) • When to use unsigned? • When you really need that extra bit • When you’ll do lots of bit shifting • When you’ll never do (a < 0) test

  22. The int Data Type • Description: A positive or negative integer • Same as signed int • Size: System dependent • 16 <= bits in short <= bits in int <= bits in long • Usually 16 bits (alias 2 bytes) or 32 bits (alias 4 bytes) • The “natural word size” of the computer

  23. Two’s complement High-order bit indicates sign Leading zero means octal Leading zero-x means hexadecimal The int Data Type (cont.) • Example constants (assuming 4 bytes)

  24. Same range as int, but shifted on number line Note “U” suffix The unsigned int Data Type • Description: A positive integer • Size: System dependent • Same as int • Example constants (assuming 4 bytes)

  25. Note “L” suffix The long Data Type • Description: A positive or negative integer • Same as signed long • Size: System dependent • 16 <= bits in short <= bits in int <= bits in long • Usually 32 bits, alias 4 bytes • Example constants (assuming 4 bytes)

  26. Note “UL” suffix The unsigned long Data Type • Description: A positive integer • Size: System dependent • Same as long • Example constants (assuming 4 bytes)

  27. No way to express constant of type short, so must use cast The short Data Type • Description: A positive or negative integer • Same as signed short • Size: System dependent • 16 <= bits in short <= bits in int <= bits in long • Usually 16 bits, alias 2 bytes • Example constants (assuming 2 bytes)

  28. No way to express constant of type unsigned short, so must use cast The unsigned short Data Type • Description: A positive integer • Size: System dependent • Same as short • Example constants (assuming 4 bytes)

  29. No way to express constant of type signed char, so must use cast The signed char Data Type • Description: A (small) positive or negative integer • Size: 1 byte • Example constants

  30. No way to express constant of type unsigned char, so must use cast The unsigned char Data Type • Description: A (small) positive integer • Size: 1 byte • Example constants

  31. The char Data Type • On some systems, char means signed char • On other systems, char means unsigned char • Obstacle to portability int a[256]; char c; c = (char)255; … … a[c] … /* char is unsigned => a[255] => OK */ /* char is signed => a[-1] => out of bounds */

  32. The char Data Type (cont.) • On your system, is char signed or unsigned? • Output on hats #include <stdio.h> int main(void) { char c = (char)0x80; if (c > 0) printf("unsigned"); else printf("signed"); return 0; } signed

  33. The char Data Type (cont.) • Q: • Why is type char called “char” (meaning “character”)? • A: • Type char can be used for limited range arithmetic • As indicated previously • However, type char is used more often to store a charactercode • As shown next lecture

  34. Summary • Computer represents everything in binary • Integers, floating-point numbers, characters, addresses, … • Pixels, sounds, colors, etc. • Binary arithmetic through logic operations • Sum (XOR) and Carry (AND) • Two’s complement for subtraction • Binary operations in C • AND, OR, NOT, and XOR • Shift left and shift right • Useful for efficient and concise code, though sometimes cryptic • C integral data types • char, short, int, long (signed and unsigned)

More Related

深圳SEO优化公司北京SEO按天扣费推荐贵阳网页制作报价泰州网站搭建价格海东SEO按天计费多少钱临沧模板网站建设黄南网站建设多少钱保山seo网站优化多少钱益阳企业网站建设公司乌海设计网站多少钱林芝企业网站改版多少钱拉萨网站改版推荐太原网站优化推广价格晋城SEO按效果付费价格梧州设计网站价格宁德网站制作多少钱大庆网站制作哪家好金华关键词排名推荐福州网站定制公司南山推广网站价格南山百搜标王哪家好坪山百度seo北海百度标王报价襄阳推广网站哪家好诸城SEO按天扣费价格上饶网站建设设计报价绵阳网页设计哪家好荆州网站改版推荐绵阳优秀网站设计价格玉溪网站搜索优化报价商丘网站定制歼20紧急升空逼退外机英媒称团队夜以继日筹划王妃复出草木蔓发 春山在望成都发生巨响 当地回应60岁老人炒菠菜未焯水致肾病恶化男子涉嫌走私被判11年却一天牢没坐劳斯莱斯右转逼停直行车网传落水者说“没让你救”系谣言广东通报13岁男孩性侵女童不予立案贵州小伙回应在美国卖三蹦子火了淀粉肠小王子日销售额涨超10倍有个姐真把千机伞做出来了近3万元金手镯仅含足金十克呼北高速交通事故已致14人死亡杨洋拄拐现身医院国产伟哥去年销售近13亿男子给前妻转账 现任妻子起诉要回新基金只募集到26元还是员工自购男孩疑遭霸凌 家长讨说法被踢出群充个话费竟沦为间接洗钱工具新的一天从800个哈欠开始单亲妈妈陷入热恋 14岁儿子报警#春分立蛋大挑战#中国投资客涌入日本东京买房两大学生合买彩票中奖一人不认账新加坡主帅:唯一目标击败中国队月嫂回应掌掴婴儿是在赶虫子19岁小伙救下5人后溺亡 多方发声清明节放假3天调休1天张家界的山上“长”满了韩国人?开封王婆为何火了主播靠辱骂母亲走红被批捕封号代拍被何赛飞拿着魔杖追着打阿根廷将发行1万与2万面值的纸币库克现身上海为江西彩礼“减负”的“试婚人”因自嘲式简历走红的教授更新简介殡仪馆花卉高于市场价3倍还重复用网友称在豆瓣酱里吃出老鼠头315晚会后胖东来又人满为患了网友建议重庆地铁不准乘客携带菜筐特朗普谈“凯特王妃P图照”罗斯否认插足凯特王妃婚姻青海通报栏杆断裂小学生跌落住进ICU恒大被罚41.75亿到底怎么缴湖南一县政协主席疑涉刑案被控制茶百道就改标签日期致歉王树国3次鞠躬告别西交大师生张立群任西安交通大学校长杨倩无缘巴黎奥运

深圳SEO优化公司 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化