Question: TASK: Write C code that performs the following steps: 1 . Include qutyio.h and , and initialise the serial interface using serial _ init

TASK: Write C code that performs the following steps:
1. Include "qutyio.h" and , and initialise the serial interface
using serial_init().
2. Create a variable "state" to store your student number in decimal.
You should use a fixed-width integer type from the header
file.
3. Iterate through all the numbers from 0 to 255 in sequence, and for
each number, perform the following steps:
a. Take the bitwise XOR of the number with the variable "state",
storing the result back into "state".
b. Rotate right the bits in "state" at least one time, and until the
LSB of "state" is a zero. If there are no cleared bits in "state"
do nothing.
c. Print the least significant two bytes of "state" to the serial
output as four uppercase hexadecimal digits, followed by a space.
The prefix "0x" is not required.
d. Inspect bits 11-4 of "state" (where bit 0 is the LSB).
- If the most significant nibble of this byte, represented as a
hexadecimal digit, matches the second last digit of your student
number, represented as a decimal digit, print the word "foo" to
the serial output.
- If the least significant nibble of this byte, represented as a
hexadecimal digit, matches the final digit of your student
number, represented as a decimal digit, print the word "bar" to
the serial output.
- If both match, print "foobar".
e. Print a newline character to the serial output.
4. After step 3, your program should have printed 256 lines to the
serial output.
Program execution should then proceed to the infinite loop without
producing any further output.
My code outputs 256 lines, however it isnt printing them in a correct order and without foo and bar on the ends?
#include
#include "qutyio.h"
#include
int main(void){
// Initialize the serial interface
serial_init();
uint32_t state =12345678; //placeholder
// Iterate through numbers from 0 to 255
for (int i =0; i <=255; i++){
state ^= i;
// Rotate the bits in 'state' to the right
while (state & 1){
state >>=1;
}
// Print the least significant two bytes of 'state'
printf("%04X ", state & 0xFFFF);
// Extract the nibbles and compare with student number digits
int ms_nibble =(state >>4) & 0xF;
int ls_nibble = state & 0xF;
int student_number_digits =5678;
if (ms_nibble == student_number_digits){
printf("foo");
}
if (ls_nibble == student_number_digits){
printf("bar");
}
printf("
");
}
// END OF EXTENSION03 EXERCISES //
// DO NOT EDIT BELOW THIS LINE //
while (1); // Loop indefinitely
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!