Machine-translated by Google. Questions are authored in English.
Top

Embedded C Programming Questions and Answers

11.

A bit is to be cleared without disturbing the others. The correct line is

Answer: C

Correct answer: reg &= ~(1u << n);

Shifting one into position and inverting gives a mask with a zero at that bit and ones everywhere else; ANDing with it clears the bit and leaves the rest untouched. OR would set the bit, XOR would toggle it, and plain assignment would destroy every other bit in the register.

Please log in or register to report this question.

Answer: D

Shifting brings the wanted bit to position zero and the mask discards everything above it, so the result is one or zero. Testing with reg & 8 instead yields eight or zero, which is also true or false in C but is not the same value.

Please log in or register to report this question.

13.

The value of y after this runs is

uint8_t y = 1;
y <<= 7;

Answer: A

Shifting one left seven places puts it in the top bit of a byte, which is 0x80. One more shift would push it out and leave zero, and a shift by eight or more of an 8-bit value is undefined behaviour in principle and unhelpful in practice.

Please log in or register to report this question.

Answer: B

A shift count at or beyond the width of the promoted operand is undefined, and real processors do different things: some give zero and some use only the low five bits of the count and shift by nothing at all. Both are conforming, which is why the case must be avoided.

Please log in or register to report this question.

15.

A mask to keep the low six bits of a byte is

Answer: C

Correct answer: 0x3F

Six bits set is 0011 1111, which is \( 0x3F \). A quick check: a mask of n low bits is \( 2^n - 1 \), so \( 64 - 1 = 63 \), and 63 in hexadecimal is \( 0x3F \). The distractor \( 0x60 \) confuses the count of bits with their positions.

Please log in or register to report this question.

16.

On a 32-bit target with natural alignment, what is sizeof(struct s)?

struct s {
    uint8_t  a;
    uint32_t b;
    uint8_t  c;
};

Answer: D

Three bytes of padding follow a so that b lands on a four-byte boundary, and three more follow c so the whole structure is a multiple of its strictest alignment. Reordering the members as b, a, c would bring it down to eight.

Please log in or register to report this question.

17.

Reordering that structure's members largest first would reduce its size to

Answer: A

With b first, a and c then occupy two of the following bytes and two bytes of tail padding complete the four-byte multiple, giving eight. Declaring members in descending order of size is a cheap habit that saves memory in any structure instantiated many times.

Please log in or register to report this question.

Answer: B

On a core that cannot fetch an unaligned word the compiler must generate several byte accesses instead of one, and on some cores an unaligned access faults outright. Packing is for matching an external format, not for saving memory in internal structures.

Please log in or register to report this question.

19.

Casting a byte buffer to a structure pointer to parse a network packet is unsafe chiefly because of

Answer: C

Correct answer: alignment and padding assumptions

The compiler inserts padding to align members, and the layout it chooses need not match the packing of the network protocol. Worse, a byte buffer may not be aligned as the structure requires, and an unaligned access faults on some processors. Parsing field by field, or a packed structure with the endianness handled explicitly, is the safe route.

Please log in or register to report this question.

Answer: D

Correct answer: view the same storage as more than one type

A union overlays its members on the same bytes, so a 32-bit word can be examined as four bytes or a float inspected bitwise. That reinterpretation is its purpose. It does not reduce alignment - a union takes the strictest requirement of its members - and only one member holds a valid value at a time.

Please log in or register to report this question.

Loading…
Tags: Embedded C Programming Questions and Answers || Embedded C Programming MCQ Questions and Answers || Embedded C Programming GK Questions and Answers || Embedded C Programming GK MCQ Questions || Embedded C Programming Multiple Choice Questions and Answers || Embedded C Programming GK || GK on Embedded C Programming || Embedded Systems Questions and Answers || Embedded Systems MCQ Questions and Answers || Embedded Systems GK Questions and Answers || GK on Embedded Systems