Complete the code to define a 16-bit integer variable named value.
uint16_t [1] = 0x1234;
The variable is named value as required.
Complete the code to create a pointer to the first byte of value.
uint8_t *ptr = (uint8_t *) &[1];The pointer ptr points to the address of value.
Fix the error in the code to correctly print the first byte of value in hexadecimal.
printf("First byte: 0x%[1]\n", *ptr);
The format specifier %X prints the number in uppercase hexadecimal.
Fill the blank to create a function that checks if the system is little-endian.
int is_little_endian() {
unsigned int num = 1;
return *((unsigned char *) &num) [1] 1;
}The function returns true if the first byte equals 1, indicating little-endian.
Fill all three blanks to create a function that swaps the byte order of a 16-bit integer.
uint16_t swap_bytes(uint16_t val) {
return (val [1] 8) | ((val [2] 8) & [3] 0xFF00);
}The function shifts the upper byte right and the lower byte left, then masks the upper byte.
