Embedded C Programming Flashcards

Cards pertaining to Embedded Programming Containing Various Lessons Learned throughout My Experiences

1
Q

UnSet a specific bit in a byte (set to 0)

A

targetByte &= ~(1 &laquo_space;n); //Shifts a 1 n bits left then inverts then AND’s the values and sets the source byte to the result.

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

Toggle a specifc bit in a byte (1=0 | 0=1)

A

targetByte ^= 1 &laquo_space;n; //Exclusive OR the bit shifted 1 with the source byte.

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

Bitwise “OR” Operator and its’ use.

A

Or Operator is the pipe : “ | “
It is used to set individual bits, for example
REG = (1«19) | (1«12); // 19th and 12th bits are set to ‘1’ , rest are Zeros.

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

Set 3rd bit of byte to 1 using bit shift…

A

(1«3)

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

Operator for inverting bits

A

”~”

Bit-wise Negation Operator

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

Set 11th and 4th bit of a register to 1…

A

REG |= (1 &laquo_space;11) | (1 &laquo_space;4);

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

Syntax and format of ternary operator…

A

result = binaryCondition ? valueReturnedIfTrue : valueReturnedIfFalse;

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

”~”

A

Bit-wise Negation Operator, used for the inversion of bits.

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

Check value of individual bit in byte

A

(targetByte&raquo_space; n) & 1; //Compares shifted value to see if AND’d byt with 0x01 returns a 1

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

Set a specific bit in a byte

A

targetByte |= (1<

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

How do you set a subset of bits in a value?

A
Bitwide OR'ing
uint8_t mask = 0x0F;    //00001111b
uint8_t value = 0x55;    //01010101b
output = mask | value; value;
output = 0x05;  //00000101b
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Proper use scenarios for the “voltaile” keyword

A

A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change:

  1. Memory-mapped peripheral registers
  2. Global variables modified by an interrupt service routine
  3. Global variables accessed by multiple tasks within a multi-threaded application
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you extract a certain subset of bits from a value?

A
Bitwide ANDing
uint8_t mask = 0x0F;    //00001111b
uint8_t value = 0x55;    //01010101b
output = mask &amp; value;
output = 0x05;  //00000101b

common uses : extracting individual bytes from a word (would utilize shifting as well)

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

Binary “AND” operator and it’s uses

A

“&” Binary AND Operator
Used to check condition of individual bits, usually performed using bit masking. For example:
reg1 = 0x2A
a = reg1 & (1 &laquo_space;3); //0x2A & 0x

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

”|=” uses

A

Used to set individual bits of a byte without interfering with the status of other bits. This is done by masking

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