lecture 7 Flashcards

integer arithmetic overflow

1
Q

signed integer arithmetic overflow

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

arithmetic overflow for signed integers in 2s complement

A

*two integers in 2s comp:
1) pos int + pos int = negative int
2) neg int + neg int = positive int

easy to detect in 2s

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

0 1 1 1
+ 0 1 1 1
————–
1 1 1 0

= 7
+ 7
———–
- 2
what is happening here?

A

arithmetic overflow

2 positive integers (msb = 0) in 2s comp are added and equal a negative value
- 7 + 7 = -2 –> doesnt make sense

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

1 0 0 0
+ 1 0 0 1
————–
0 0 0 1

= - 8
+ - 7
———–
1
what is happening here?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

arithmetic overflow for unsigned integers

A

*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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

what happens with the msb during arithmetic overflow?

A

if the msb carry-out bit = 1 –> arithmetic overflow

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

1 1^1 1 0 0
+ 0 1 0 0
—————-
0 0 0 0

what is happening here?

A

arithmetic overflow

the msb carry-out bit = 1 which means there is arithmetic overflow happening

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

what is the msb carry-out bit if there is no arithmetic overflow happening?

A

0
*what we want

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

int i = 10;
unsigned int j = (unsigned int) i;

what is happening here?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

short a = -1;
unsigned short b = (unsigned short)a;
printf(“b = %u\n”, b);

what is happening here?

A

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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

1 0 1 1
- (8) + 2 + 1
= -5

*after casted as unsigned:
1 0 1 1
= ?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

what is integer casting good for with signed data types?

A

absolute value

use when data type is signed (pos, neg, and zero)
**converts negative to positive without changing the value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

x &laquo_space;y

A

left shift
shifts x to the left by y positions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

is bitwise left shift logical, arithmetic, or both?

A

logical ONLY

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

x &laquo_space;y

what are the 3 steps for bitwise left shift?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

x : 0 1 1 0 0 0 1 0
x &laquo_space;3:

what is the final binary number?

A

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
17
Q

x&raquo_space; y

A

bitwise right shift
shifts x to the right by y positions

18
Q

is bitwise right shift logical, arithmetic, or both?

A

both
logical shift –> only if x is unsigned
arithmetic shift –> only if x is signed

19
Q

what data type gets logically right shifted?

A

unsigned (pos, zero)

20
Q

what data type gets arithmetically right shifted?

A

signed (neg, pos, zero)

21
Q

x : 1 0 1 0 0 0 1 0
x&raquo_space; 2:

what is the final binary number after a logical right shift?

A

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

22
Q

x : 1 0 1 0 0 0 1 0
x&raquo_space; 2:

what is the final binary number after an arithmetic right shift?

A

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

23
Q

bitwise multiplication

A

u &laquo_space;k
= u* 2^k

u &laquo_space;3
= u * 2^3

24
Q

bitwise division

A

u&raquo_space; k
= u / 2^k
*floor function –> round up

u&raquo_space; 3
= u / 2^3

25
Q

A & B

A

bitwise “and”

26
Q

A | B

A

bitwise “or”

27
Q

~A

A

bitwise “not”

28
Q

A ^ B

A

bitwise “xor”

29
Q

A & B = 1
when A = ? and B = ?

A

when both A AND B = 1

30
Q

A | B = 1
when A = ? and B = ?

A

when A = 1 or B = 1 or both = 1

31
Q

A = 1
~A = ?

A

A = 0, complement:
1 –> 0
0 –> 1

32
Q

A ^ B = 1
when A = ? and B = ?

A

when A = 1 or B = 1

  • NOT BOTH –> ONLY ONE OR THE OTHER
33
Q

what is the useful application for ANDing?

A

masking certain bits and clearing certain bits

**can select certain bits to change/keep by using &

34
Q

what is the useful application for ORing?

A

setting certain bits

35
Q

what is the useful application for XORing?

A

complementing certain bits