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

Embedded C Programming Questions and Answers

Answer: A

Whether the first declared field occupies the low or the high bits, and how fields straddle a unit boundary, are implementation defined, so the same declaration maps differently on two compilers. Masks and shifts on an explicit unsigned type are portable where bit fields are not.

Please log in or register to report this question.

22.

What does this print on a 32-bit target?

int      i = -1;
unsigned u =  1;
printf("%s\n", i < u ? "less" : "not less");

Answer: B

Correct answer: not less

The usual arithmetic conversions turn the signed operand into unsigned when the two have equal rank, so \( -1 \) becomes 4,294,967,295 and is not less than 1. The code compiles and its behaviour is perfectly defined - it simply does something other than what it appears to say, which is why compilers warn about mixed comparisons.

Please log in or register to report this question.

23.

The value of c after this runs on a target with 32-bit int is

uint8_t a = 200, b = 100;
uint8_t c = (a + b) / 2;

Answer: C

Both operands are promoted to int before the addition, so the sum is 300 with no wrap and the division gives 150, which fits in a byte. Had the arithmetic been done in eight bits the sum would have wrapped to 44 and the answer would have been 22.

Please log in or register to report this question.

Answer: D

Unsigned arithmetic wraps by definition; signed overflow is undefined, and modern optimisers exploit that to assume it cannot happen. Code that relies on signed wrapping can therefore be transformed in surprising ways.

Please log in or register to report this question.

Answer: A

Correct answer: the counter can never reach 300 and the loop never ends

A uint8_t holds 0 to 255, so after 255 it wraps to 0 and the condition remains true for ever. The comparison itself is well defined, since the counter is promoted to int; the fault is that the type cannot represent the bound, which is a common and hard-to-spot error in constrained code.

Please log in or register to report this question.

Answer: C

A core without a floating point unit emulates every operation in software at tens of cycles each, whereas fixed point arithmetic is ordinary integer work. The costs are a limited dynamic range and the discipline of tracking the scaling.

Please log in or register to report this question.

Answer: D

Correct answer: \( -1 \) to just under \( +1 \)

Q15 places the binary point immediately below the sign bit, so the 16-bit integer is read as a fraction: the range runs from \( -1 \) to \( 1 - 2^{-15} \). The format is used where fractional arithmetic is needed on a processor with no floating point unit.

Please log in or register to report this question.

29.

The value 0.5 in Q15 format is stored as the integer

Answer: A

Correct answer: 16384

The integer stored is the fraction multiplied by \( 2^{15} \): \[ 0.5 \times 32768 = 16384. \] Note that \( +1 \) itself cannot be represented, since \( 1.0 \times 32768 = 32768 \) exceeds the range of a signed 16-bit value - which is why the upper bound is just below one.

Please log in or register to report this question.

Answer: B

The fractional bits add, so the product of two Q15 values has thirty fractional bits and needs a 32-bit accumulator. Shifting right by fifteen returns it to Q15, and doing so before the multiplication instead loses the precision the accumulator preserved.

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