jeudi 26 février 2015

C Manipulate Bits Within a Bit String



I am currently working on a problem where I am trying to manipulate bits within a bit string.


I am trying to take a signal mask given as:


00101001


and check it against a signal mask given as an integer using the AND and OR operators.


I have some code currently that takes an unsigned character and prints out its binary representation, however I'm not sure how to continue.



main() {
int signal = 2;
char a = 41;

int i;
for (i = 0; i < 8; i++) {
printf("%d \n", !!((a << i) & 0x80));
}
printf("\n");

return 0;
}



  • The purpose of the program is supposed to take the signal number given, set all bits equal to 0 except the number bit that is the signal number and use an AND operator to find out if the signal is set or not based on whether the value is 0 or 1.


Can anybody help me out with this problem?




Aucun commentaire:

Enregistrer un commentaire