Enums and bitwise operators Flashcards

1
Q

How to declare an enum?

A

enum tag { INT, DOUBLE]

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

How do you initialize an enum?

A

enum tag { A = 1, B = 2, C, D = -6, F } // Not really initializing

Each of the elements are assigned numbers in increasing order.

By default, the first element will be given 0.

enum tag newtag = A; // initializing

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

How do you print out an enum?

A

enum tag newtag = INT

printf(“%d”, t); // prints out the number associated with that enum

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

What should you watch out for when naming or tagging a struct, union or enums.

A

Structs, unions and enums share the same namespace so their names can collide.

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

Where would you use enums inside a struct?

A

Enums can be used to as a tag to for a struct with a union.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  • w1 & w2
  • w1 | w2
A
  • Bitwise and - mask one value with another
  • Bitwise or - merge together 2 values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  • w1 ^ w2
  • ¬ (tilde) w
A
  • bitwise exclusive or between 2 values
  • ones complement invert
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  • w &= 0xff
  • b |= w & 0xff
A
  • Need to check
  • Merge b with the last byte of x ( need to check if it is supposed to be 0xff or something else )
How well did you know this?
1
Not at all
2
3
4
5
Perfectly