In one byte i set some bits
|Video | Audio | speaker | mic | Headphone | Led with bits
|1 | 1 | 1 | 3 | 1 | 1
1 byte for all except for mic which has 3 bytes and thus can have 7 combination leaving
the first combination.
#define Video 0x01
#define Audio 0x02
#define Speaker 0x04
#define MicType1 0x08
#define MicType2 0x10
#define MicType3 0x20
#define MicType4 (0x08 | 0x10)
#define MicType5 (0x08 | 0x20)
#define MicType6 (0x10 | 0x20)
#define MicType7 ((0x08 | 0x10) | 0x20)
#define HeadPhone 0x40
#define Led 0x80
Now I set the bits
MySpecs[2] |= (1 << 0);
MySpecs[2] |= (1 << 2);
//set mictype6
MySpecs[2] |= (1 << 4);
MySpecs[2] |= (1 << 5);
when I do read like this
readCamSpecs()
{
if(data[0] & Video)
printf("device with Video\n");
else
printf("device with no Video\n");
if(data[0] & Audio)
printf("device with Audio\n");
else
printf("device with no Audio\n");
if(data[0] & Mictype7)
printf("device with Mictype7\n");
if(data[0] & Mictype6)
printf("device with Mictype6\n");
}
The values set with single bits, it can find.
But the values set with multiple bits (e.g, MicType5,6,7) it makes error
and displays whatever is the first in check.
What am I doing wrong?
Your
&check succeeds even when there’s only one bit set, as the result would still be non-zero.Try
if ( data[0] & Mictype7 == MicType7 )instead.