integer size in C++ unsigned int all allowed to be bigger minimum size required by standard for all C++ compilers sign) “unsigned” — can’t be negative (no 64 32 unsigned long 32 16 16 varies between machines 16 unsigned short 8 8 unsigned char on lab machines minimum type size in bits compiler uses what makes most sense on each machine? 20
querying sizes in C++ #include <climits> // C: <limits.h> ... // e.g. USHRT_MAX == 65535 on lab machines #include <limits> ... std::numeric_limits< unsigned long >::max() // == ULONG_MAX ... sizeof ( unsigned long ) // == 8 on lab machines ... 21 ULONG_MAX or UINT_MAX or USHRT_MAX or UCHAR_MAX // number of *bytes*
numbering bits two viable ways to number bits do I have a way to ask for bit ? does it matter which I use? 22 option 1: n -bit number: b n − 1 b n − 2 b n − 3 . . . b 2 b 1 b 0 n − 1 b i · 2 i = � i =0 option 2: n -bit number: b 0 b 1 b 2 . . . b n − 3 b n − 2 b n − 1 n − 1 b i · 2 n − i − 1 = � i =0
numbering bits two viable ways to number bits do I have a way to ask for bit ? does it matter which I use? 22 option 1: n -bit number: b n − 1 b n − 2 b n − 3 . . . b 2 b 1 b 0 n − 1 b i · 2 i = � i =0 option 2: n -bit number: b 0 b 1 b 2 . . . b n − 3 b n − 2 b n − 1 n − 1 b i · 2 n − i − 1 = � i =0
numbering bits two viable ways to number bits does it matter which I use? 22 option 1: n -bit number: b n − 1 b n − 2 b n − 3 . . . b 2 b 1 b 0 n − 1 b i · 2 i = � i =0 option 2: n -bit number: b 0 b 1 b 2 . . . b n − 3 b n − 2 b n − 1 n − 1 b i · 2 n − i − 1 = � i =0 do I have a way to ask for bit i ?
numbering bytes option 2: 4-byte number: in memory, yes — each byte needs an address (number) does it matter which I use? two viable ways to number bytes option 1: 4-byte number: 23 B 3 B 2 B 1 B 0 3 B i · 256 i = � i =0 B 0 B 1 B 2 B 3 3 b i · 256 3 − i = � i =0
numbering bytes option 2: 4-byte number: in memory, yes — each byte needs an address (number) does it matter which I use? two viable ways to number bytes option 1: 4-byte number: 23 B 3 B 2 B 1 B 0 3 B i · 256 i = � i =0 B 0 B 1 B 2 B 3 3 b i · 256 3 − i = � i =0
numbering bytes option 2: 4-byte number: in memory, yes — each byte needs an address (number) does it matter which I use? two viable ways to number bytes option 1: 4-byte number: 23 B 3 B 2 B 1 B 0 3 B i · 256 i = � i =0 B 0 B 1 B 2 B 3 3 b i · 256 3 − i = � i =0
if big endian if little endian memory 0 3 0 2 0 1 0 addr.value (8-bit) 4 (as 8-bit values) … … … 11 0 5 0 0 (as 8-bit values) … … … 11 0 10 9 10 0 8 95 7 228 6 1 4 238 addr.value 95 (64-bit) 0 123999 8 323232 16 434093 … … memory (as 64-bit values) addr.value 9 0 (8-bit) 1 4 160 8 0 7 0 6 0 5 0 0 228 3 1 2 24 1 · 256 2 123999 = 228 · 256 1 + 95 · 256 0 +
memory 0 0 3 0 2 0 1 0 (8-bit) 0 addr.value (as 8-bit values) if little endian … … … 11 4 5 10 10 (as 8-bit values) if big endian … … … 11 0 0 1 9 0 8 95 7 228 6 4 238 addr.value 95 (64-bit) 0 123999 8 323232 16 434093 … … memory (as 64-bit values) addr.value 9 0 (8-bit) 1 4 160 8 0 7 0 6 0 5 0 0 228 3 1 2 24 1 · 256 2 123999 = 228 · 256 1 + 95 · 256 0 +
fjnding endianness in C++ big endian: based on which end we call “part 0” but we don’t write numbers in a difgerent order (afgects overall value the most) big endian: byte 0 is most signifjcant (afgects overall value the least) little endian: byte 0 is least signifjcant (cast to int to output as number, not character) use pointer to get i th byte of value won’t see endianness unless you do something like this lowest address in value get pointer to byte with 1 23 45 67 89 ab cd ef 123456789abcdef ef cd ab 89 67 45 23 1 #include <iostream> 123456789abcdef little endian (e.g. lab machine): } ... } "; cout << ( int ) ptr[i] << " for ( int i = 0; i < sizeof ( unsigned long ); ++i) { cout << hex << value << endl; unsigned long value = 0x0123456789ABCDEF; int main() { using std::cout; using std::hex; using std::endl; 25 unsigned char *ptr = ( unsigned char *) &value; ␣
fjnding endianness in C++ big endian: based on which end we call “part 0” but we don’t write numbers in a difgerent order (afgects overall value the most) big endian: byte 0 is most signifjcant (afgects overall value the least) little endian: byte 0 is least signifjcant (cast to int to output as number, not character) use pointer to get i th byte of value won’t see endianness unless you do something like this lowest address in value get pointer to byte with 1 23 45 67 89 ab cd ef 123456789abcdef ef cd ab 89 67 45 23 1 #include <iostream> 123456789abcdef little endian (e.g. lab machine): } ... } "; cout << ( int ) ptr[i] << " for ( int i = 0; i < sizeof ( unsigned long ); ++i) { cout << hex << value << endl; unsigned long value = 0x0123456789ABCDEF; int main() { using std::cout; using std::hex; using std::endl; 25 unsigned char *ptr = ( unsigned char *) &value; ␣
fjnding endianness in C++ big endian: based on which end we call “part 0” but we don’t write numbers in a difgerent order (afgects overall value the most) big endian: byte 0 is most signifjcant (afgects overall value the least) little endian: byte 0 is least signifjcant (cast to int to output as number, not character) use pointer to get i th byte of value won’t see endianness unless you do something like this lowest address in value get pointer to byte with 1 23 45 67 89 ab cd ef 123456789abcdef ef cd ab 89 67 45 23 1 #include <iostream> 123456789abcdef little endian (e.g. lab machine): } ... } "; cout << ( int ) ptr[i] << " for ( int i = 0; i < sizeof ( unsigned long ); ++i) { cout << hex << value << endl; unsigned long value = 0x0123456789ABCDEF; int main() { using std::cout; using std::hex; using std::endl; 25 unsigned char *ptr = ( unsigned char *) &value; ␣
fjnding endianness in C++ big endian: based on which end we call “part 0” but we don’t write numbers in a difgerent order (afgects overall value the most) big endian: byte 0 is most signifjcant (afgects overall value the least) little endian: byte 0 is least signifjcant (cast to int to output as number, not character) use pointer to get i th byte of value won’t see endianness unless you do something like this lowest address in value get pointer to byte with 1 23 45 67 89 ab cd ef 123456789abcdef ef cd ab 89 67 45 23 1 #include <iostream> 123456789abcdef little endian (e.g. lab machine): } ... } "; cout << ( int ) ptr[i] << " for ( int i = 0; i < sizeof ( unsigned long ); ++i) { cout << hex << value << endl; unsigned long value = 0x0123456789ABCDEF; int main() { using std::cout; using std::hex; using std::endl; 25 unsigned char *ptr = ( unsigned char *) &value; ␣
fjnding endianness in C++ big endian: based on which end we call “part 0” but we don’t write numbers in a difgerent order (afgects overall value the most) big endian: byte 0 is most signifjcant (afgects overall value the least) little endian: byte 0 is least signifjcant (cast to int to output as number, not character) use pointer to get i th byte of value won’t see endianness unless you do something like this lowest address in value get pointer to byte with 1 23 45 67 89 ab cd ef 123456789abcdef ef cd ab 89 67 45 23 1 #include <iostream> 123456789abcdef little endian (e.g. lab machine): } ... } "; cout << ( int ) ptr[i] << " for ( int i = 0; i < sizeof ( unsigned long ); ++i) { cout << hex << value << endl; unsigned long value = 0x0123456789ABCDEF; int main() { using std::cout; using std::hex; using std::endl; 25 unsigned char *ptr = ( unsigned char *) &value; ␣
fjnding endianness in C++ big endian: based on which end we call “part 0” but we don’t write numbers in a difgerent order (afgects overall value the most) big endian: byte 0 is most signifjcant (afgects overall value the least) little endian: byte 0 is least signifjcant (cast to int to output as number, not character) use pointer to get i th byte of value won’t see endianness unless you do something like this lowest address in value get pointer to byte with 1 23 45 67 89 ab cd ef 123456789abcdef ef cd ab 89 67 45 23 1 #include <iostream> 123456789abcdef little endian (e.g. lab machine): } ... } "; cout << ( int ) ptr[i] << " for ( int i = 0; i < sizeof ( unsigned long ); ++i) { cout << hex << value << endl; unsigned long value = 0x0123456789ABCDEF; int main() { using std::cout; using std::hex; using std::endl; 25 unsigned char *ptr = ( unsigned char *) &value; ␣
fjnding endianness in C++ big endian: based on which end we call “part 0” but we don’t write numbers in a difgerent order (afgects overall value the most) big endian: byte 0 is most signifjcant (afgects overall value the least) little endian: byte 0 is least signifjcant (cast to int to output as number, not character) use pointer to get i th byte of value won’t see endianness unless you do something like this lowest address in value get pointer to byte with 1 23 45 67 89 ab cd ef 123456789abcdef ef cd ab 89 67 45 23 1 #include <iostream> 123456789abcdef little endian (e.g. lab machine): } ... } "; cout << ( int ) ptr[i] << " for ( int i = 0; i < sizeof ( unsigned long ); ++i) { cout << hex << value << endl; unsigned long value = 0x0123456789ABCDEF; int main() { using std::cout; using std::hex; using std::endl; 25 unsigned char *ptr = ( unsigned char *) &value; ␣
little versus big endian little endian — least signifjcant part has lowest address i.e. index 0 is the one’s place big endian — most signifjcant part has the lowest address 26 i.e. index n − 1 is the one’s place
endianness in the real world today and this course: little endian is dominant e.g. x86, typically ARM historically: big endian was dominant e.g. typically SPARC, POWER, Alpha, MIPS, … still commonly used for networking because of this many architectures have switchable endianness e.g. ARM, SPARC, POWER, MIPS usually, OS chooses one endianness 27
middle endian sometimes not just big/little endian e.g. number bytes most to least signifjcant as 5, 6, 7, 8, 1, 2, 3, 4 e.g. doubles on little-endian ARM generally some sort of historical accident e.g. ARM fmoating point designed for big endian? 28
endianness is about addresses 6 … … 11 … 10 4 9 238 8 160 7 0 0 … 5 0 4 0 3 0 2 1 1 228 0 95 = addr.value … endianness is about numbering, 4 not (necessairily) placement on the page but, probably assume English order (left to right, etc.) if not otherwise specifjed addr.value 0 95 1 228 2 1 3 0 0 … 5 0 6 0 7 0 8 160 9 238 10 4 11 29
endianness and bit-order we won’t talk about bit order because bits don’t have addresses signifjcant”? nothing about how pointers, etc. work suggests either answer is correct 30 if I say “bit 0 ”, question: “numbering from least signifjcant or most
endianness and writing out bytes 0x0102 in binary: 00000001000000010 English’s order — most signifjcant fjrst bytes of 0x0102 in big endian: (byte 0) 00000001 (byte 1) 00000010 bytes of 0x0102 in little endian: (byte 0) 00000010 (byte 1) 00000001 usually , we don’t change the order we write bits if writing out bytes, fjrst in reading order is usually lowest address (we’ll specify if not) 31
representing negative numbers 0 and bigger signed: negative numbers, but how? sign & magnitude 1’s complement 2’s complement 000…000 011…111 goal: same bits, signed or not 100…000 111…111 two representations of zero? x == y needs to do something special more negative values than positive values? all 1’s — least negative? all 1’s — most negative? unsigned: positive numbers up to unsigned integers 0000…0000 0000…0000 1111…1111 0111…1111 1000…0000 = ??? signed integers = 1111…1111 = ??? 0111…1111 = 1000…0000 32 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1
representing negative numbers 0 and bigger signed: negative numbers, but how? sign & magnitude 1’s complement 2’s complement 000…000 011…111 goal: same bits, signed or not 100…000 111…111 two representations of zero? x == y needs to do something special more negative values than positive values? all 1’s — least negative? all 1’s — most negative? unsigned: positive numbers up to unsigned integers 0000…0000 0000…0000 1111…1111 0111…1111 1000…0000 = ??? signed integers 1111…1111 = ??? 0111…1111 1000…0000 32 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1 = 0 = 2 n − 1 − 1
representing negative numbers 0 and bigger signed: negative numbers, but how? sign & magnitude 1’s complement 2’s complement 000…000 011…111 goal: same bits, signed or not 100…000 111…111 two representations of zero? x == y needs to do something special more negative values than positive values? all 1’s — least negative? all 1’s — most negative? unsigned: 32 unsigned integers 0000…0000 0000…0000 1111…1111 0111…1111 1000…0000 = ??? signed integers 1111…1111 = ??? 0111…1111 1000…0000 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1 positive numbers up to 2 n − 1 = 0 = 2 n − 1 − 1
representing negative numbers 011…111 signed: negative numbers, but how? sign & magnitude 1’s complement 2’s complement 000…000 0 100…000 positive numbers up to 111…111 two representations of zero? x == y needs to do something special more negative values than positive values? all 1’s — least negative? all 1’s — most negative? goal: same bits, signed or not = ??? unsigned integers 0000…0000 0000…0000 1111…1111 0111…1111 1000…0000 1000…0000 signed integers 1111…1111 = ??? 0111…1111 32 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1 unsigned: 2 n − 1 and bigger = 0 = 2 n − 1 − 1
representing negative numbers 0 and bigger signed: negative numbers, but how? unsigned integers 1’s complement 2’s complement 000…000 011…111 goal: same bits, signed or not 100…000 111…111 two representations of zero? x == y needs to do something special more negative values than positive values? all 1’s — least negative? all 1’s — most negative? unsigned: sign & magnitude positive numbers up to = 0000…0000 = 1111…1111 = 0111…1111 = 1000…0000 = ??? 32 signed integers 1111…1111 0000…0000 1000…0000 0111…1111 = ??? 0 0 2 n − 1 − 1 2 n − 1 − 1 2 n − 1 − 1 − 2 n − 1 + 1 − 2 n − 1 0 − 2 n − 1 + 1 0 − 1 = 0 = 2 n − 1 − 1
representing negative numbers 0 and bigger signed: negative numbers, but how? unsigned integers 1’s complement 2’s complement 000…000 011…111 goal: same bits, signed or not 100…000 111…111 two representations of zero? x == y needs to do something special more negative values than positive values? all 1’s — least negative? all 1’s — most negative? unsigned: sign & magnitude positive numbers up to = 0000…0000 = 1111…1111 = 0111…1111 = 1000…0000 = ??? 32 signed integers 1111…1111 0000…0000 1000…0000 0111…1111 = ??? 0 0 2 n − 1 − 1 2 n − 1 − 1 2 n − 1 − 1 − 2 n − 1 + 1 − 2 n − 1 0 − 2 n − 1 + 1 0 − 1 = 0 = 2 n − 1 − 1
representing negative numbers 0 and bigger signed: negative numbers, but how? unsigned integers 1’s complement 2’s complement 000…000 011…111 goal: same bits, signed or not 100…000 111…111 two representations of zero? x == y needs to do something special more negative values than positive values? all 1’s — least negative? all 1’s — most negative? unsigned: sign & magnitude positive numbers up to = 0000…0000 = 1111…1111 = 0111…1111 = 1000…0000 = ??? 32 signed integers 1111…1111 0000…0000 1000…0000 0111…1111 = ??? 0 0 2 n − 1 − 1 2 n − 1 − 1 2 n − 1 − 1 − 2 n − 1 + 1 − 2 n − 1 0 − 2 n − 1 + 1 0 − 1 = 0 = 2 n − 1 − 1
representing negative numbers 0 and bigger signed: negative numbers, but how? unsigned integers 1’s complement 2’s complement 000…000 011…111 goal: same bits, signed or not 100…000 111…111 two representations of zero? x == y needs to do something special more negative values than positive values? all 1’s — least negative? all 1’s — most negative? unsigned: sign & magnitude positive numbers up to = 0000…0000 = 1111…1111 = 0111…1111 = 1000…0000 = ??? 32 signed integers 1111…1111 0000…0000 1000…0000 0111…1111 = ??? 0 0 2 n − 1 − 1 2 n − 1 − 1 2 n − 1 − 1 − 2 n − 1 + 1 − 2 n − 1 0 − 2 n − 1 + 1 0 − 1 = 0 = 2 n − 1 − 1
representing negative numbers 0 and bigger signed: negative numbers, but how? unsigned integers 1’s complement 2’s complement 000…000 011…111 goal: same bits, signed or not 100…000 111…111 two representations of zero? x == y needs to do something special more negative values than positive values? all 1’s — least negative? all 1’s — most negative? unsigned: sign & magnitude positive numbers up to = 0000…0000 = 1111…1111 = 0111…1111 = 1000…0000 = ??? 32 signed integers 1111…1111 0000…0000 1000…0000 0111…1111 = ??? 0 0 2 n − 1 − 1 2 n − 1 − 1 2 n − 1 − 1 − 2 n − 1 + 1 − 2 n − 1 0 − 2 n − 1 + 1 0 − 1 = 0 = 2 n − 1 − 1
sign and magnitude fmip sign bit to negate number 1000…0101 = 0000…0101 = 1000…0000 0111…1111 1111…1111 0000…0000 difgerent direction if negative unsigned integers adding 1 fjrst bit is “sign bit” — 0 = positive, 1 = negative 1000…0000 0111…1111 1111…1111 0000…0000 33 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1 = − 0 = − 2 n − 1 + 1 = +0 = 2 n − 1 − 1
sign and magnitude fjrst bit is “sign bit” — 0 = positive, 1 = negative 1000…0000 0111…1111 1111…1111 0000…0000 difgerent direction if negative unsigned integers fmip sign bit to negate number adding 1 1000…0000 0111…1111 1111…1111 0000…0000 33 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1 1000…0101 = − 6 0000…0101 = 6 = − 0 = − 2 n − 1 + 1 = +0 = 2 n − 1 − 1
sign and magnitude fjrst bit is “sign bit” — 0 = positive, 1 = negative 1000…0000 0111…1111 1111…1111 0000…0000 difgerent direction if negative unsigned integers fmip sign bit to negate number adding 1 = 1000…0000 = 0111…1111 = 1111…1111 = 0000…0000 33 1000…0101 = − 6 0000…0101 = 6 = − 0 = − 2 n − 1 + 1 = +0 = 2 n − 1 − 1
sign and magnitude fmip sign bit to negate number 1000…0101 = 0000…0101 = 1000…0000 0111…1111 1111…1111 0000…0000 difgerent direction if negative unsigned integers adding 1 fjrst bit is “sign bit” — 0 = positive, 1 = negative = 1000…0000 = 0111…1111 = 1111…1111 = 0000…0000 33 = − 0 = − 2 n − 1 + 1 = +0 = 2 n − 1 − 1
1’s complement adding 1 1111…1010 = 0000…0101 = 1000…0000 = 0111…1111 1111…1111 0000…0000 unsigned integers same direction, no matter original sign fmip all bits to negate number 1000…0000 0111…1111 1111…1111 0000…0000 34 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1 = − 2 n − 1 − 1 = +0 = − 0 n − 1 − 1
1’s complement fmip all bits to negate number 1000…0000 = 0111…1111 1111…1111 0000…0000 unsigned integers adding 1 same direction, no matter original sign 1000…0000 0111…1111 1111…1111 0000…0000 34 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1 1111…1010 = − 6 0000…0101 = 6 = − 2 n − 1 − 1 = +0 = − 0 n − 1 − 1
1’s complement fmip all bits to negate number 1000…0000 = 0111…1111 1111…1111 0000…0000 unsigned integers adding 1 same direction, no matter original sign = 1000…0000 = 0111…1111 = 1111…1111 = 0000…0000 34 1111…1010 = − 6 0000…0101 = 6 = − 2 n − 1 − 1 = +0 = − 0 n − 1 − 1
1’s complement adding 1 1111…1010 = 0000…0101 = 1000…0000 = 0111…1111 1111…1111 0000…0000 unsigned integers same direction, no matter original sign fmip all bits to negate number = 1000…0000 = 0111…1111 = 1111…1111 = 0000…0000 34 = − 2 n − 1 − 1 = +0 = − 0 n − 1 − 1
two’s complement adding 1 1111…1010 = 0000…0101 = 1000…0000 0111…1111 1111…1111 0000…0000 unsigned integers same direction, no matter original sign fmip all bits and add 1 to negate number 1000…0000 0111…1111 1111…1111 0000…0000 35 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1 = − 2 n − 1 = +0 = − 1 = 2 n − 1 − 1
two’s complement fmip all bits and add 1 to negate number 1000…0000 0111…1111 1111…1111 0000…0000 unsigned integers adding 1 same direction, no matter original sign 1000…0000 0111…1111 1111…1111 0000…0000 35 = 2 n − 1 = 2 n − 1 = 0 = 2 n − 1 − 1 1111…1010 = − 6 0000…0101 = 6 = − 2 n − 1 = +0 = − 1 = 2 n − 1 − 1
two’s complement fmip all bits and add 1 to negate number 1000…0000 0111…1111 1111…1111 0000…0000 unsigned integers adding 1 same direction, no matter original sign = 1000…0000 = 0111…1111 = 1111…1111 = 0000…0000 35 1111…1010 = − 6 0000…0101 = 6 = − 2 n − 1 = +0 = − 1 = 2 n − 1 − 1
two’s complement adding 1 1111…1010 = 0000…0101 = 1000…0000 0111…1111 1111…1111 0000…0000 unsigned integers same direction, no matter original sign fmip all bits and add 1 to negate number = 1000…0000 = 0111…1111 = 1111…1111 = 0000…0000 35 = − 2 n − 1 = +0 = − 1 = 2 n − 1 − 1
2’s complement (alt. perspective) -10 = s place” “ 2’s complement (5 bit) + + + + + + 0 1 +10 = 0 1 1 36 + 0 + 1 + 0 1 0 0 · ( − 2 4 ) + + 0 · 2 2 + + 1 · 2 1 + + 1 · 2 3 0 · 2 0 + 0 = 10 0 2 3 0 2 1 1 · ( − 2 4 ) + 0 · 2 3 1 · 2 2 1 · 2 1 0 · 2 0 − 2 4 + 0 = − 10 0 2 2 2 1
2’s complement (alt. perspective) + 2’s complement (5 bit) + + + + + + 0 1 +10 = 0 1 -10 = 1 36 0 + + 0 1 0 1 0 · ( − 2 4 ) + + 0 · 2 2 + + 1 · 2 1 + + 1 · 2 3 0 · 2 0 + 0 = 10 0 2 3 0 2 1 1 · ( − 2 4 ) + 0 · 2 3 1 · 2 2 1 · 2 1 0 · 2 0 − 2 4 + 0 = − 10 0 2 2 2 1 “ − 2 4 s place”
add a d d add o r or add unsigned v. 2’s complement 111 is same as unsigned addition 2’s complement addition 110 101 100 011 010 001 000 37 0 or 0 7 or − 1 1 or 1 6 or − 2 2 or 2 5 or − 3 3 or 3 4 or − 4
a d d add o r or add unsigned v. 2’s complement 111 is same as unsigned addition 2’s complement addition 110 100 101 011 010 001 000 37 add 1 0 or 0 7 or − 1 1 or 1 6 or − 2 2 or 2 5 or − 3 3 or 3 4 or − 4
add or add unsigned v. 2’s complement 100 is same as unsigned addition 2’s complement addition 110 101 111 37 010 011 000 001 add 1 0 or 0 a d 7 or − 1 1 or 1 d 7 o r − 1 6 or − 2 2 or 2 5 or − 3 3 or 3 4 or − 4
add a d d o r or add unsigned v. 2’s complement 111 is same as unsigned addition 2’s complement addition 110 101 100 011 010 001 000 37 0 or 0 7 or − 1 1 or 1 add 1 6 or − 2 2 or 2 5 or − 3 3 or 3 4 or − 4
add a d d o r unsigned v. 2’s complement 101 is same as unsigned addition 2’s complement addition 111 110 011 100 010 001 000 37 0 or 0 7 or − 1 1 or 1 add 1 6 or − 2 2 or 2 5 or − 3 3 or 3 add 7 or − 1 4 or − 4
add a d d o r unsigned v. 2’s complement 101 is same as unsigned addition 2’s complement addition 111 110 011 100 010 001 000 37 0 or 0 7 or − 1 1 or 1 add 1 6 or − 2 2 or 2 5 or − 3 3 or 3 add 7 or − 1 4 or − 4
other 2’s complement arithmetic subtraction also the same as unsigned multiplication — repeated addition — mostly the same (but need some extra precision) 38
Recommend
More recommend