Please help. I am new to C. I am trying to display the character 'W' as it is written in binary using a bitmask. I am aware there are alternate ways to do this, but I want to know why this did not work. If you read the comments, I know the problem is somewhere in the 'if' statement, because the code works when I delete the if statement. The compilers error message says: ...17 (line of the if statement): "invalid operands to binary & (having 'int' and 'char *)". Yet letter & array are both char variables. So I do not understand:
Code:
#include <stdio.h>
int main(void){
int i = 0;
char letter = 'W';
char array[] = "0b00000000";
char *pointer = array;
// so first, i want the pointer + 2 to change the 0 (in array) to a 1. But I want this to be an increment, not a pointer switch.
for (i = 2; i < 10; i++){
*(pointer + i) = '1';
// Now I have the bitmask. So I want to apply the bitmask to W using a boolean operator, and print either a 1 or 0 according
// to which bit value is contained in 'W'. I also want to undo the '1' in the bitmask at the end of the loop, setting it to 0, then moving
// on to the next bit in the bitmask; changing that to a 1, and repeating the process until all 8 bits have been checked.
if (letter & array){
printf("1");
}
// So at this point array = 0b10000000. The printf statement confirms that.
//
// I am aware this code is not complete. I'd planned to add the statement:
// if (~(letter & array)){
// printf("0");
// }
printf("%s", array);
// If I do not include the 'if' statement, the printf function runs and displays my bitmasks, incremented from 0b10000000 to 0b01000000
//and so on. But, when I add the if statement, it no longer works.
*(pointer + i) = '0';
}
return 0;
}