to each of the data types meant to represent integers on the machine where the

To each of the data types meant to represent integers

This preview shows page 7 - 12 out of 36 pages.

to each of the data types meant to represent integers, on the machine where the program is run. If, on a particular machine, n is the number of bytes allocated to such a type, then that type is guaranteed to encompass all integers from - 2 8 n - 1 + 1 up to 2 8 n - 1 - 1, on that particular machine, as confirmed by ranges_and_integers.c . In order to be portable , a program should not use the actual number of bytes that is allocated to any type meant to represent integers, but only the requirements set by the standard that a short should be at least 16 bits wide, a long at least 32 bits wide, and a long long at least 64 bits wide; the range of a type is included in the range of a larger type. Type _Bool is not a type of its own: it is synonymous to int . COMP9021 (Principles of Programming) Notes 5.0: Arithmetic types, constants, printf() 2014 session 1 7 / 36
Image of page 7

Subscribe to view the full document.

Internal representation of integers (1) The internal representation of integers is usually given by their twos-complement notation , even though other representations are possible, such as one-complement notation or sign magnitude notation ; that notation allows one to also represent - 2 8 n - 1 on a machine where n bytes are allocated to the type. The leftmost bit is set to 0 for a positive number, and set to 1 for a negative number. A representation of a negative number N can be obtained from the representation of the absolute value of N by changing all 0s to 1s, all 1s to 0s, and then adding 1. For instance, if we had one byte to represent -119 in twos-complement notation, we would proceed as follows. Represent 119: 0 1 1 1 0 1 1 1 Change 0s to 1s and 1s to 0s: 1 0 0 0 1 0 0 0 Add 1: 1 0 0 0 1 0 0 1 COMP9021 (Principles of Programming) Notes 5.0: Arithmetic types, constants, printf() 2014 session 1 8 / 36
Image of page 8
Internal representation of integers (2) On machines where 16 bits are allocated to a signed short int , particular representations include: 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 : -32768 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 0 : -138 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 : -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 : 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 : 1 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 : 138 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 : 32767 COMP9021 (Principles of Programming) Notes 5.0: Arithmetic types, constants, printf() 2014 session 1 9 / 36
Image of page 9

Subscribe to view the full document.

Types for real numbers The program bytes_and_real_numbers.c indicates how many bytes are allocated to each of the data types meant to represent real numbers (which of course can only be rational), on the machine where the program is run. Some of the information that characterises the types meant to represent real numbers is displayed by ranges_and_real_numbers.c . The standard requires that the range of a type is included in the range of a larger type. COMP9021 (Principles of Programming) Notes 5.0: Arithmetic types, constants, printf() 2014 session 1 10 / 36
Image of page 10
Internal representation of normalised real numbers Take a nonzero rational number r that in base 2, is of the form ± 1 . b 1 . . . b 23 2 e with b 1 , . . . , b 23 in { 0 , 1 } and e in { - 126 , . . . , 127 } .
Image of page 11

Subscribe to view the full document.

Image of page 12
  • Fall '19
  • 11

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes