0
0
Embedded Cprogramming~10 mins

Left shift and right shift behavior in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to left shift the variable value by 2 bits.

Embedded C
int value = 5;
int result = value [1] 2;
Drag options to blanks, or click blank then click option'
A-
B>>
C+
D<<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the right shift operator instead of left shift.
Using addition instead of bit shifting.
2fill in blank
medium

Complete the code to right shift the variable value by 3 bits.

Embedded C
unsigned int value = 32;
unsigned int result = value [1] 3;
Drag options to blanks, or click blank then click option'
A<<
B>>
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using left shift instead of right shift.
Using subtraction instead of bit shifting.
3fill in blank
medium

Fix the error in the code to correctly perform a left shift by 1 bit.

Embedded C
int value = 10;
int result = value [1] 1;
Drag options to blanks, or click blank then click option'
A<<
B>>
C+
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using right shift operator instead of left shift.
Using arithmetic operators instead of bit shift.
4fill in blank
hard

Fill both blanks to complete the loop that left shifts numbers greater than 3 by 2 bits into the shifted array.

Embedded C
int numbers[] = {1, 2, 3, 4, 5};
int shifted[5];
int idx = 0;
for(int i = 0; i < 5; i++) {
    if(numbers[i] [1] 3) {
        shifted[idx++] = numbers[i] [2] 2;
    }
}
Drag options to blanks, or click blank then click option'
A>
B<<
C<
D>>
Attempts:
3 left
💡 Hint
Common Mistakes
Using less than instead of greater than in the condition.
Using right shift instead of left shift.
5fill in blank
hard

Fill all three blanks to create a loop that right shifts each number by 1 bit if it is less than 10, otherwise left shifts by 1 bit.

Embedded C
int numbers[] = {5, 10, 15};
int results[3];
for(int i = 0; i < 3; i++) {
    if(numbers[i] [1] 10) {
        results[i] = numbers[i] [2] 1;
    } else {
        results[i] = numbers[i] [3] 1;
    }
}
Drag options to blanks, or click blank then click option'
A<
B>>
C<<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using greater than instead of less than in the condition.
Mixing up left and right shift operators.