You are writing code to control 8 stage lights in the Performing Arts Center. You start by accessing an 8-bit unsigned char value through a pointer called register. This value is used to turn lights on (1) and off (0). The diagram below is the bit map of a sample value for the stage lights, but the actual value will differ.
bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
0 1 1 0 1 0 0 1
Your job is to turn off the two end lights (bit0 and bit7). After the declaration of the pointer register, write code to set bit0 and bit7 of the value that register points to, to a 0.
unsigned char * register; register=malloc(sizeof(char));
*register=0x85;
Make sure the other lights are maintained, and only bit0 and bit7 lights are changed switched off.
//write code here
Please find the answer below
1. We can use hexadecimal value 0x7E to disable the first and last light for the stage performance.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int bitSet(unsigned char reg, int bit)
{
if (reg & (1 << (bit)))
return 1;
else
return 0;
}
int main()
{
unsigned char *registe = NULL;
registe=(unsigned char *)malloc(sizeof(char));
*registe=0x85 & 0x7E;
// printf("%x", *registe);
printf("Light Number |bit7 |bit6 |bit5
|bit4 | bit3| bti2|bit1 |bit0 |\n");
printf("--------------|-----|-----|-----|-----|-----|-----|-----|-----|\n");
printf("Status
| %d | %d | %d | %d
| %d | %d | %d
| %d |",
bitSet(*registe, 7),
bitSet(*registe, 6),
bitSet(*registe, 5),
bitSet(*registe, 4),
bitSet(*registe, 3),
bitSet(*registe, 2),
bitSet(*registe, 1),
bitSet(*registe, 0));
printf("\n");
return 1;
}
Output:
USER >./a.out
Light Number |bit7 |bit6 |bit5 |bit4 | bit3| bti2|bit1 |bit0
|
--------------|-----|-----|-----|-----|-----|-----|-----|-----|
Status | 0
| 0 | 0 | 0 | 0
| 1 | 0 | 0 |
You are writing code to control 8 stage lights in the Performing Arts Center. You start by accessing an 8-bit unsigned char value through a pointer called register. This value is used to turn lights o...