0% found this document useful (0 votes)
5 views2 pages

Bit Manipulation in Embedded Systems

Uploaded by

bombatingz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Bit Manipulation in Embedded Systems

Uploaded by

bombatingz
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Department of Electrical Engineering

Embedded Systems (ELEC366)

Tutorial 8: Bit Manipulation


For all questions, show all your work in detail

1. (a) Write a ‘C’ code fragment to swap bytes of a 16-bit unsigned number and
display the result in Hexadecimal
Byte 1 Byte 0

Byte 0 Byte 1

(b) Write a function that can be used to concatenate two unsigned 8-bit
variables.
.
2. Examine the circuit diagram below along with the corresponding program:
#define DIP_START_PIN 2
0 Vbus
#define NUM_SWITCHES 8 1 Vsys
void setup() { GND GND
2 3V3 en
[Link](9600); 3 Vout
4 Vref
while(!Serial);

Raspberry Pi Pico W
5 28
GND GND
init_dip_switch_pins(); 6 27
} 7 26
8 Run
void init_dip_switch_pins(void) { 9 22
GND GND
for (int i = 0; i < NUM_SWITCHES; i++) 10 21
{ 11
12
20
19
gpio_init(DIP_START_PIN + i); 13 18
GND GND
gpio_set_dir(DIP_START_PIN + i, GPIO_IN); 14 17
gpio_pull_up(DIP_START_PIN + i); 15 16

}
}
int dip_switch_dec(void) {
int decimal = 0, weight = 1;
for (int i = 0; i < NUM_SWITCHES; i++) {
decimal += (!gpio_get(DIP_START_PIN + i)) * weight;
weight *= 2;
}
return decimal;
}
void loop() {
int value = dip_switch_dec();
[Link](value);
sleep_ms(2000);
}
Optimize the program’s performance by incorporating bitwise manipulation methods.
3. A single-precision floating-point number in C follows the IEEE 754 standard and is
represented using 32 bits, divided into three fields (sign, exponent, and mantissa). Write a C
program that takes a single-precision floating-point number as input and extracts its sign bit,
exponent, and mantissa:

1. Prompt the user to enter a floating-point number.


2. Display the binary representation of the number.
3. Extract and print the binary values of the sign, exponent, and mantissa separately.

The program must generate the output shown below, and it should use a single display
function to print the binary forms of the entered floating-point number, its sign, exponent,
and mantissa.

4. A Bluetooth-enabled oximeter, a device that


measures blood oxygen saturation (SpO₂) and
heart rate (HR), transmits these measurements
once per second to a Bluetooth-enabled
microcontroller using three bytes of data (see
Figure 1). As illustrated in Figure 2, the three Pulse Oximeter mbed
bytes contain seven bits representing SpO₂
(SP0–SP6) and nine bits representing HR
(HR0–HR8), while the bits marked ‘X’ are
reserved for other purposes.
Figure 1

Byte 1 x x x x x x HR8 HR7

Byte 2 x HR6 HR5 HR4 HR3 HR2 HR1 HR0

Byte 3 x SP6 SP5 SP4 SP3 SP2 SP1 SP0

Figure 2

Given that three variables, B1, B2, and B3, are declared as unsigned char in the
loop() function to store byte 1, byte 2, and byte 3 received by the microcontroller, write C
code to extract the SpO₂ and HR values from these bytes.

You might also like