
Originally Posted by
olegharput
how can we bitwise OR these Hex. numbers(unsigned integers) in C?
0x00000001u
0x00000010u
0x00000020u
0x00000200u
0x00001000u
0x00002000u
0x00004000u
0x00008000u
i want to use the result in another language so please explain how to comment
integer and unsigned integer because they confuse my mind for ex. 34 is integer and unsigned integer at the same time?
It's quite common in C to need a fairly large number of boolean
parameters. Instead of passing large numbers of parameters
individually, it is no uncommon to wrap them up into a single
integer, with each flag being a bit of that integer. So all the
flags are powers of two.
Note, I've called it an "integer", but it isn't really an integer. A real
integer counts something, and these integers don't. They are just
collections of bits, their value is arbitrary and of no meaning. To
indicate that, the convention is to declare them "unsigned".
Normally you have fewer than 32 flags and the top bit is always
zero, so it's just a convention, it won't actually make any difference
if you make your value a plain "int".
The bitwise "or" operator is '|' (vertical bar). It's a single vertical
bar. Normally you #define your flags. So you can construct a bitset with
Code:
/* all powers of two. It doesn't really matter which bits you choose */
#define FLAG1 0x01
#define FLAG2 0x02
#define FLAG3 0x04
unsigned int flags = FLAG1 | FLAG2 | FLAG3;
Note the order of the flags doesn't matter.
Now to test it we do the folowing
Code:
if (flag & FLAG1)
/* execute code for flag 1 set */