If I have an 8 bit number like so, 00101001,
- how would I replace one of the bits with a 1 (setting the bit)?
- how would I replace one of the bits with a 0?
Currently in my code I have two functions as shown:
void ADDSET(unsigned char *signalmask, int SIGNUM) {
*signalmask = *signalmask | (SIGNUM);
}
void DELSET(unsigned char *signalmask, int SIGNUM) {
*signalmask = *signalmask &~ SIGNUM;
}
With a function call like so:
int main() {
unsigned char signalmask = 41;
int SIGNUM = 7; // signal 1-8
ADDSET(&signalmask, SIGNUM);
return 0;
}
For some reason when I output my signalmask's memory address it either doesn't replace/remove the bit, or it does the wrong one. I think this is because the SIGNUM is starting at 0 when it should be starting at 1.
I output using:
for (i = 0; i < 8; i++) {
printf("%d", !!((signalmask << i) & 0x80));
}
Can anybody help me?
Note: My INSET function works as desired:
int INSET(unsigned char signalmask, int SIGNUM){
unsigned char bitMask = 1 << (SIGNUM - 1);
if ((signalmask & bitMask) == 0)
return 0;
else
return 1;
}
My problem is I don't know how to port this code over to the ADDSET and DELSET functions.
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire