16-bit Memory Word CSCI 125 & 161 / ENGR 144 • To store number 6, use 0000000000000110 Lecture 5 • Value 0 is 0000000000000000 Martin van Bommel • Largest value is 1111111111111111 = 65,535 = 2 16 − 1 • 32-bit word gives largest value > 4 billion Storing Negative Values Two’s Complement • Sign Magnitude • Two’s complement – use first bit as sign bit, 0 = positive 1 = negative – if positive, use binary – e.g. 8-bits – if negative, complement bits and add one 00000000 = 0 10000000 = −0 – e.g. −53 00000001 = 1 10000001 = −1 magnitude 00110101 … … complement 11001010 01111111 = 127 11111111 = −127 add 1 11001011 8-bit Two’s Complement Two’s Complement Addition 00000000 = 0 • Add −1 + 2? 00000001 = 1 11111111 = −1 −1 11111111 00000010 = 2 11111110 = −2 +2 00000010 … … ---- ------------ +1 � 01111111 = 127 10000001 = −127 00000001 10000000 = −128 1
16-bit Two’s Complement Data Types • 8-bit two’s complement range is • Two properties − 2 7 = − 128 to 2 7 − 1 = 127 – set of values (domain) – set of operations • 16-bit two’s complement range is − 2 15 = −32,768 to 2 15 − 1 = 32,767 • int - 32-bit two’s complement integers - 2 147 483 648 to 2 147 483 647 +, -, *, /, % • 32-bit two’s complement range is − 2 31 � −2 million to 2 31 − 1 � 2 million Floating-point Data Character Data • Numbers that include fractional parts • char - Elements from the ASCII table • Values expressed in single quotes e.g. ’A’ ’?’ ’\n’ • float - 32-bit floating point representation • stored in memory as their ASCII value – approximately seven decimal digits of accuracy • double - 64-bit floating point representation – approximately sixteen decimal digits of accuracy +, - +, -, *, / Expressions Constants • A sequence of terms and operators • Explicit values that never change • Term - represents single data value • Integer constants - sequence of digits – constant - data value as part of program, e.g. 4 – e.g. 1000; 5; 1 not 1,000; 3.14 – variable - name as placeholder for memory • Floating-point constant - includes decimal – function call - one that generates a value – e.g. 3.14; 1.0; 2.9E+8 e.g. sqrt(9) – expression in parenthesis • String constant - characters in double quotes • Operator - character indicating computation – e.g. “Hello, world.\n” 2
Variables Variable Names • Placeholder for value in memory • Must start with letter or underscore ( _ ) followed by letters, digits, or underscores – name - alias for memory location – value - current value stored in location • Case of letters significant – type - kind of data values that may be stored • Name not a C ++ keyword • Name any length, but only 31 characters significant (safe only up to 8 characters) name value Operators and Operands Combining Integers and Floats • expression operator expression • Result of expression is most specific type • e.g. (2 * x) + (3 * y) • e.g. Value of 2.5 + 3 is 5.5 • expressions are operands - values to which operator is applied • operator signifies operation • type of expression n + 1 is type of n • binary operators - two operands e.g. a + b • type of expression n + 1.5 is float if n is float or int, but double if n is double • unary operators - one operand e.g. - b Integer Division and Remainder Precedence Rules • Division of int’s results in int • Parenthesized expressions first • Remainder operator % gives remainder • Unary minus operator next • *, /, and % next, from left to right • e.g. 5 / 2 is 2 5 % 2 is 1 • + and - next, from left to right • 5 / 2.0 is 2.5 0 % 4 is 0 • e.g. 8 * (7 - 6 + 5) % (4 + 3 / 2) - 1 • 5.0 / 2 is 2.5 1 % 4 is 1 • 5.0 / 2.0 is 2.5 4 % 4 is 0 3
Recommend
More recommend