lecture 7 Flashcards
integer arithmetic overflow
signed integer arithmetic overflow
when two signed integers are added and the result is either:
- exceeds the max value
- less than the min value
that can be represented by the number of bits (N)
arithmetic overflow for signed integers in 2s complement
*two integers in 2s comp:
1) pos int + pos int = negative int
2) neg int + neg int = positive int
easy to detect in 2s
0 1 1 1
+ 0 1 1 1
————–
1 1 1 0
= 7
+ 7
———–
- 2
what is happening here?
arithmetic overflow
2 positive integers (msb = 0) in 2s comp are added and equal a negative value
- 7 + 7 = -2 –> doesnt make sense
1 0 0 0
+ 1 0 0 1
————–
0 0 0 1
= - 8
+ - 7
———–
1
what is happening here?
arithmetic overflow
2 negative integers (msb = 1) in 2s comp are added and equal a positive value
- 8 + - 7 = 1 –> doesnt make sense
**msb carry-out bit = 1 –> another sign of arithmetic overflow
arithmetic overflow for unsigned integers
*unsigned –> only positive integers or zero (no negatives)
1) unsigned int + unsigned int = result
- result is greater than the max
value that can be represented
for that number of bits
what happens with the msb during arithmetic overflow?
if the msb carry-out bit = 1 –> arithmetic overflow
1 1^1 1 0 0
+ 0 1 0 0
—————-
0 0 0 0
what is happening here?
arithmetic overflow
the msb carry-out bit = 1 which means there is arithmetic overflow happening
what is the msb carry-out bit if there is no arithmetic overflow happening?
0
*what we want
int i = 10;
unsigned int j = (unsigned int) i;
what is happening here?
integer casting from signed to unsigned
int i is declared as a signed integer
- signed (pos, neg, zero)
unsigned int j declared as unsigned integer
- unsigned (pos and zero ONLY)
- assigning int j to the unsigned
version of int i
** casting i as an unsigned int
set as j
short a = -1;
unsigned short b = (unsigned short)a;
printf(“b = %u\n”, b);
what is happening here?
short a –> signed short (pos, neg, zero)
- set to -1
unsigned short b = (unsigned short)a
- casting signed short to unsigned
short and puts that result into b
b –> expects -1 to be printed, but since it’s casted as unsigned (no neg):
65535 is printed
why? –> 0xFFFF = -1 in 2s comp
F F F F 1111 1111 1111 1111 = 65535 bc no longer represents a negative number --> becomes the largest value for unsigned ints with 16 bits
1 0 1 1
- (8) + 2 + 1
= -5
*after casted as unsigned:
1 0 1 1
= ?
8 + 2 + 1 = 11
changes -8 to 8 since it is now unsigned (cannot be negative)
*msb = 1 –> not negative in this instance bc unsigned cannot represent a negative integer
what is integer casting good for with signed data types?
absolute value
use when data type is signed (pos, neg, and zero)
**converts negative to positive without changing the value
x «_space;y
left shift
shifts x to the left by y positions
is bitwise left shift logical, arithmetic, or both?
logical ONLY
x «_space;y
what are the 3 steps for bitwise left shift?
1) shift x to the left by y positions
2) throw away the excess bits on the left
“shifted out”
3) pad the right with 0s until full