Let's suppose I have unsigned char
array called backup where I want to copy some bytes. But what I want to do is to clear most of the bits (18 bits from right to left). What I need to stay is the two bits on the left. This is my try:
memcpy (buf, " AA", 3);
unsigned char backup[3];
memcpy( backup, buf, 3 ); // backup
int two_bits_backup;
two_bits_backup = (int) backup &= ~0b001111111111111111111111;
But it generates error: lvalue required as left operand of assignment
How to clear the two bits successfully?
What I want to do is to clear these bits (bold)
in case two_bits_backup = 0b111111111111111111111111;
case two_bits_backup == 0b110000000000000000000000;
I wanted to convert the unsigned char array to int and then to clear it.
I am using this on C and 32-bit platform, where int has 4 bytes.
backup
is an unsigned char array, you can't just treat it as if it's anint
. (in other words,(int)backup
is very wrong and it's not doing anything like what you want.) – Steve Summitbackup
in your code is not anunsigned char *
. – melpomene