Lecture 8: Bitwise Operations in C and UNIX Flashcards

1
Q

Btiwse AND operator

A

&

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

How to mask something with 0s?

A

Bitwise AND.

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

Bitwise assignmetn

A

Example: AND
x &= int;

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

Octal format specifer.

A

%o

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

Bitwise OR operator

A

|

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

XOR operator

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

XOR to swap 2 variables

A

i1 ^= i2
i2 ^= i1
i1 ^= i2
This lets i1 and i2 be swapped without a temporary variable

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

What does ~ do?

A

Ones complement

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

Ones complement operator

A

~

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

Left and right shift operators.

A

&laquo_space;and&raquo_space;
As I recall, A«B would shift A left by B bits

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

Does left and right shift work with assignment?

A

Yes.
A«=1;

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

Multiplying/dividing by 2 with bit shifts.

A

Shift left to multiply by 2.
Shift right to multiply by 2.
Also could be extended to all powers of 2.

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

Arithmetic vs Logical right shift.

A

If there is a 0 in the left-most bit, it is always filled with a 0. If there is a 1, arithmetic fills with 1s but logical fills with 0s. This varies from machine to machine, so never assume which behavior will be used there.

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

What happens if you try to bit-shift by a negative amount?

A

Undefined behavior.

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

How might you find if something is a power of 2 with bitwise operators?

A

Figure it out.

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