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
x : 0 1 1 0 0 0 1 0
x «_space;3:
what is the final binary number?
0 1 1 0 0 0 1 0
0 0 0 1 0 0 0 0
= 00010000
- shifted x to the left by 3 positions starting at the LSB
- 0 0 0 1 0 - extra bits to the left get thrown away
- 0 1 1 - padded the open positions with 0s
- 0 0 0
x»_space; y
bitwise right shift
shifts x to the right by y positions
is bitwise right shift logical, arithmetic, or both?
both
logical shift –> only if x is unsigned
arithmetic shift –> only if x is signed
what data type gets logically right shifted?
unsigned (pos, zero)
what data type gets arithmetically right shifted?
signed (neg, pos, zero)
x : 1 0 1 0 0 0 1 0
x»_space; 2:
what is the final binary number after a logical right shift?
logical shift:
0 0 1 0 1 0 0 0
shift the bits to the right by 2
positions starting at the msb
- 1 0 1 0 0 0
throw away the leftover bits to the
right
- 1 0
pad the open positions with 0s
- 0 0
x : 1 0 1 0 0 0 1 0
x»_space; 2:
what is the final binary number after an arithmetic right shift?
arithmetic shift:
1 1 1 0 1 0 0 0
shift the bits to the right by 2
positions starting at the msb
- 1 0 1 0 0 0
throw away the leftover bits to the
right
- 1 0
pad the open positions with 1s to
preserve the sign
- 1 1
bitwise multiplication
u «_space;k
= u* 2^k
u «_space;3
= u * 2^3
bitwise division
u»_space; k
= u / 2^k
*floor function –> round up
u»_space; 3
= u / 2^3
A & B
bitwise “and”
A | B
bitwise “or”
~A
bitwise “not”
A ^ B
bitwise “xor”
A & B = 1
when A = ? and B = ?
when both A AND B = 1
A | B = 1
when A = ? and B = ?
when A = 1 or B = 1 or both = 1
A = 1
~A = ?
A = 0, complement:
1 –> 0
0 –> 1
A ^ B = 1
when A = ? and B = ?
when A = 1 or B = 1
- NOT BOTH –> ONLY ONE OR THE OTHER
what is the useful application for ANDing?
masking certain bits and clearing certain bits
**can select certain bits to change/keep by using &
what is the useful application for ORing?
setting certain bits
what is the useful application for XORing?
complementing certain bits