Hi,
If I have this:
unsigned char Information[2];
Information[0] = 0x03;
Information[1] = 0x91;

Now I would like to do:
if (bit 3 == 1)
DoSomething()
else
DSomethingElse()

how do I check a specific bit?

thanks!

Re: simple bit question by muchan

muchan
Tue May 11 03:37:59 CDT 2004

Niklas wrote:

> Hi,
> If I have this:
> unsigned char Information[2];
> Information[0] = 0x03;
> Information[1] = 0x91;
>
> Now I would like to do:
> if (bit 3 == 1)
> DoSomething()
> else
> DSomethingElse()
>
> how do I check a specific bit?
>
> thanks!
>

operator & is used for masking bits.

if ( your_value & 0x04 ) // 0x04 is binary 0100

if bit 3 is zero, then masked value is 0x00.
if bit 3 is 1, then masked value is 0x40.


muchan





Re: simple bit question by Jeff

Jeff
Tue May 11 07:16:53 CDT 2004


"Niklas" <anonymous@discussions.microsoft.com> wrote in message
news:%23YNm7lyNEHA.3380@TK2MSFTNGP11.phx.gbl...
> Hi,
> If I have this:
> unsigned char Information[2];
> Information[0] = 0x03;
> Information[1] = 0x91;

I'm confused as to what the above has to do with the code below?

> Now I would like to do:
> if (bit 3 == 1)
> DoSomething()
> else
> DSomethingElse()
>
> how do I check a specific bit?

Use std::bitset like so(untested):

#include <bitset>

int main()
{
std::bitset<16> bits("1110010001"); // or use ( long( ( 0x03 << 8 ) &
0x91 ) ) );

if( bits[3] )
{
DoSomething();
}
else
{
DoSomethingElse();
}

return 0;
}

Jeff F



Re: simple bit question by Niklas

Niklas
Tue May 11 07:25:12 CDT 2004

I'm still trying to get this to work properly.
I want to check Information[0] bit 0
and Information[1] bit 4

(bit 1-3 of Information[1] can be 010 and 001, but that is not relevant to
me at this point)

these are my cases
if (Information[0] bit 0 == 0 && Information[0] bit 4 == 1)
doFirstThing();
if (Information[0] bit 1 == 1 && Information[0] bit 4 == 1)
doSecondThing();
if (Information[0] bit 1 == 1 && Information[0] bit 4 == 0)
doThirdThing();

so first I think I can do
if (Information[0] & 0x0 && Information[1] & 0x8)

second statment
if (Information[0] & 0x1 && Information[1] & 0x8)
is that correct?
but the third. how do I know if bit 4 is 0?

thanks

"muchan" <usenet@usenet.usenet> wrote in message
news:Gh0oc.2435$37.337912@news.siol.net...
> Niklas wrote:
>
> > Hi,
> > If I have this:
> > unsigned char Information[2];
> > Information[0] = 0x03;
> > Information[1] = 0x91;
> >
> > Now I would like to do:
> > if (bit 3 == 1)
> > DoSomething()
> > else
> > DSomethingElse()
> >
> > how do I check a specific bit?
> >
> > thanks!
> >
>
> operator & is used for masking bits.
>
> if ( your_value & 0x04 ) // 0x04 is binary 0100
>
> if bit 3 is zero, then masked value is 0x00.
> if bit 3 is 1, then masked value is 0x40.
>
>
> muchan
>
>
>
>



Re: simple bit question by anonymous

anonymous
Tue May 11 07:51:07 CDT 2004

Use 0x01 to check for bit 0,
0x02 for bit 1
0x04 for bit 2
0x08 for bit
0x10 for bit
Or use (1 << n) for bit n

if (Information[0] & 0x10
Bit4IsSet()
if (!(Information[0] & 0x10)
Bit4IsNotSet()


----- Niklas wrote: ----

I'm still trying to get this to work properly
I want to check Information[0] bit
and Information[1] bit

(bit 1-3 of Information[1] can be 010 and 001, but that is not relevant t
me at this point

these are my case
if (Information[0] bit 0 == 0 && Information[0] bit 4 == 1
doFirstThing()
if (Information[0] bit 1 == 1 && Information[0] bit 4 == 1
doSecondThing()
if (Information[0] bit 1 == 1 && Information[0] bit 4 == 0
doThirdThing()

so first I think I can d
if (Information[0] & 0x0 && Information[1] & 0x8

second statmen
if (Information[0] & 0x1 && Information[1] & 0x8
is that correct
but the third. how do I know if bit 4 is 0

thank

"muchan" <usenet@usenet.usenet> wrote in messag
news:Gh0oc.2435$37.337912@news.siol.net..
> Niklas wrote
>>> Hi
>> If I have this
>> unsigned char Information[2]
>> Information[0] = 0x03
>> Information[1] = 0x91
>>>> Now I would like to do
>> if (bit 3 == 1
>> DoSomething(
>> els
>> DSomethingElse(
>>>> how do I check a specific bit
>>>> thanks
>>>> operator & is used for masking bits
>> if ( your_value & 0x04 ) // 0x04 is binary 010
>> if bit 3 is zero, then masked value is 0x00
> if bit 3 is 1, then masked value is 0x40
>>> mucha
>>>>

Re: simple bit question by muchan

muchan
Tue May 11 09:11:29 CDT 2004

HS wrote:

> Use 0x01 to check for bit 0,
> 0x02 for bit 1
> 0x04 for bit 2
> 0x08 for bit 3
> 0x10 for bit 4
> Or use (1 << n) for bit n.
>
> if (Information[0] & 0x10)
> Bit4IsSet();
> if (!(Information[0] & 0x10))
> Bit4IsNotSet();
>

Ha! I didn't know that the last bit was called bit 0...
So it's 0 based problem on the terminology. 8)

muchan


Re: simple bit question by muchan

muchan
Tue May 11 09:29:42 CDT 2004

Niklas wrote:

> I'm still trying to get this to work properly.
> I want to check Information[0] bit 0
> and Information[1] bit 4
>
> (bit 1-3 of Information[1] can be 010 and 001, but that is not relevant to
> me at this point)
>
> these are my cases
> if (Information[0] bit 0 == 0 && Information[0] bit 4 == 1)
> doFirstThing();
> if (Information[0] bit 1 == 1 && Information[0] bit 4 == 1)
> doSecondThing();
> if (Information[0] bit 1 == 1 && Information[0] bit 4 == 0)
> doThirdThing();
>
> so first I think I can do
> if (Information[0] & 0x0 && Information[1] & 0x8)
>

Now I'm not sure any more what you meant with "bit 0" and "bit 3"...

but if (a_value & 0x0) doesn't work. it's always 0x0.

> second statment
> if (Information[0] & 0x1 && Information[1] & 0x8)
> is that correct?
> but the third. how do I know if bit 4 is 0?
>
> thanks
>

You mask the bit and the result is 0, then the bit is 0.

(suppose your "bit 4" means binary 10000, 0x10)

if ( Information[0] & 0x10 == 0 )

First, I think you should check what bit wise & operator does.
Second, I think you should make a utility functions returning bool like:

bool bit_0(insigned char uc) { return ( uc & 0x01 ); } // binary 0000 0001
bool bit_1(insigned char uc) { return ( uc & 0x02 ); } // binary 0000 0010
bool bit_2(insigned char uc) { return ( uc & 0x04 ); } // binary 0000 0100
bool bit_3(insigned char uc) { return ( uc & 0x08 ); } // binary 0000 1000
bool bit_4(insigned char uc) { return ( uc & 0x10 ); } // binary 0001 0000
bool bit_5(insigned char uc) { return ( uc & 0x20 ); } // binary 0010 0000
bool bit_6(insigned char uc) { return ( uc & 0x40 ); } // binary 0100 0000
bool bit_7(insigned char uc) { return ( uc & 0x80 ); } // binary 1000 0000

then

if ( bit_0(uc1) == false && bit_4(uc2) == true) ...
if ( bit_0(uc1) == true && bit_4(uc2) == true) ...
if ( bit_0(uc1) == true && bit_4(uc2) == false) ...


muchan (don't know if 0x01 is "bit 0" or "bit 1" yet...)












Re: simple bit question by Niklas

Niklas
Wed May 12 01:07:04 CDT 2004

thanks for all the help!


"muchan" <usenet@usenet.usenet> wrote in message
news:pr5oc.2462$37.340818@news.siol.net...
> Niklas wrote:
>
> > I'm still trying to get this to work properly.
> > I want to check Information[0] bit 0
> > and Information[1] bit 4
> >
> > (bit 1-3 of Information[1] can be 010 and 001, but that is not relevant
to
> > me at this point)
> >
> > these are my cases
> > if (Information[0] bit 0 == 0 && Information[0] bit 4 == 1)
> > doFirstThing();
> > if (Information[0] bit 1 == 1 && Information[0] bit 4 == 1)
> > doSecondThing();
> > if (Information[0] bit 1 == 1 && Information[0] bit 4 == 0)
> > doThirdThing();
> >
> > so first I think I can do
> > if (Information[0] & 0x0 && Information[1] & 0x8)
> >
>
> Now I'm not sure any more what you meant with "bit 0" and "bit 3"...
>
> but if (a_value & 0x0) doesn't work. it's always 0x0.
>
> > second statment
> > if (Information[0] & 0x1 && Information[1] & 0x8)
> > is that correct?
> > but the third. how do I know if bit 4 is 0?
> >
> > thanks
> >
>
> You mask the bit and the result is 0, then the bit is 0.
>
> (suppose your "bit 4" means binary 10000, 0x10)
>
> if ( Information[0] & 0x10 == 0 )
>
> First, I think you should check what bit wise & operator does.
> Second, I think you should make a utility functions returning bool like:
>
> bool bit_0(insigned char uc) { return ( uc & 0x01 ); } // binary 0000
0001
> bool bit_1(insigned char uc) { return ( uc & 0x02 ); } // binary 0000
0010
> bool bit_2(insigned char uc) { return ( uc & 0x04 ); } // binary 0000
0100
> bool bit_3(insigned char uc) { return ( uc & 0x08 ); } // binary 0000
1000
> bool bit_4(insigned char uc) { return ( uc & 0x10 ); } // binary 0001
0000
> bool bit_5(insigned char uc) { return ( uc & 0x20 ); } // binary 0010
0000
> bool bit_6(insigned char uc) { return ( uc & 0x40 ); } // binary 0100
0000
> bool bit_7(insigned char uc) { return ( uc & 0x80 ); } // binary 1000
0000
>
> then
>
> if ( bit_0(uc1) == false && bit_4(uc2) == true) ...
> if ( bit_0(uc1) == true && bit_4(uc2) == true) ...
> if ( bit_0(uc1) == true && bit_4(uc2) == false) ...
>
>
> muchan (don't know if 0x01 is "bit 0" or "bit 1" yet...)
>
>
>
>
>
>
>
>
>
>
>



Re: simple bit question by muchan

muchan
Wed May 12 06:34:48 CDT 2004

Niklas wrote:

> thanks for all the help!
>

May I suggest one more? You asked about "bit 4" etc. so I didn't talk,
but I think your each bits has actual meaning as flag, then it's
even better to give the desctiptive name for the flags.

Example:

#define FLAG_COLOR 0x04 // binary 0000 0100
#define FLAG_ROUNDEDGE 0x01 // binary 0000 0001

then you mask

if ( uc & FLAG_COLOR) {
// do something with color
}
if ( uc & FLAG_ROUNDEDGE) {
// do something with roundedge
}

or making function (or inline function) like

bool isColor (unsigned int attrib) { return (attrib & FLAG_COLOR); }
bool isRoundEdge(unsigned int attrib) { return (attrib & FLAG_ROUNDEDGE); }

if ( isColor(uc)) {
// do something with color
}
if ( isRoundEdge(uc)) {
// do something with roundedge
}

or, further, encapsulate the attribes in a class

class Attrib {
// ...

public:
bool isColor() const;
bool isRoundEdge() const;
// ...

private:
unsigned int val;
};

Attrib at;
// ....
if (at.isColor() ) {
// do something with color
}
if (at.isEoundEdge()) {
// do something with roundedge
}

This way the things becomes more intuitive, human readable and so
easier to maintain later.


muchan






Re: simple bit question by Simon

Simon
Wed May 12 19:36:04 CDT 2004


"muchan" <usenet@usenet.usenet> wrote in message
news:pr5oc.2462$37.340818@news.siol.net...
> Second, I think you should make a utility functions returning bool like:
>
> bool bit_0(insigned char uc) { return ( uc & 0x01 ); } // binary 0000
0001
> bool bit_1(insigned char uc) { return ( uc & 0x02 ); } // binary 0000
0010
> bool bit_2(insigned char uc) { return ( uc & 0x04 ); } // binary 0000
0100
> bool bit_3(insigned char uc) { return ( uc & 0x08 ); } // binary 0000
1000
> bool bit_4(insigned char uc) { return ( uc & 0x10 ); } // binary 0001
0000
> bool bit_5(insigned char uc) { return ( uc & 0x20 ); } // binary 0010
0000
> bool bit_6(insigned char uc) { return ( uc & 0x40 ); } // binary 0100
0000
> bool bit_7(insigned char uc) { return ( uc & 0x80 ); } // binary 1000
0000

Or:

template <int bitnum> bool is_set(unsigned char uc)
{
return uc & (0x01 << bitnum) != 0;
}

Of course this definition (and yours) applies only when bit 0 is defined as
the LSb of the byte.

S.