# Thread: Bitwise Operators

1. ## Bitwise Operators

Hi,

I have a function (prototype):

Code:
`void MouseButtonDown(int x, int y, int iBut:2);`

Code:
```void MouseButtonDown(int x, int y, int iBut)
{
switch (iBut)
{
case (00):
{
etc...```
Is this valid?

2. What are you trying to achieve? I believe your function prototype is syntactically invalid.

3. I'm with laserlight: Unless you have compiler extensions that allow you to pass values like that, the code should not compile.

If you are using C++, you could use templates to implement numbers that are specific number of bits, but there's no space benefit in doing so.

Bitfields are only allowed in structs/classes (and perhaps unions).

--
Mats

4. He probably wants to have a 2bit variable. I don't think that is possible.
As far as I know you can have bit fields only in a struct. Like:
Code:
```typdef struct {
int twoBit:2;
} smallInt;```
Now, the point is that you will declare the function normally to accept an int. Then you will make a struct that has one variable with :2. So it will be a struct which has a twoBit variable. Then you will pass the smallInt.twoBit int to the function and you will have what you want. You won't do though case (00): You will do normally case (0), case(1), case(2), case(3).
If you assign a value greater than 3 on a 2 bit int then only the 2 last digits will be assign, thus, since 4 = 100 you will have 00, thus your int will be 0.

Finally note that you might gain space like this, even though the compiler with default options will change the struct to a fixed size of bytes, you might not gain performance with using just one variable. But the logic is the same as having a 2 bit variable.

5. Originally Posted by C_ntua
He probably wants to have a 2bit variable. I don't think that is possible.
As far as I know you can have bit fields only in a struct. Like:
Code:
```typdef struct {
int twoBit:2;
} smallInt;```
Now, the point is that you will declare the function normally to accept an int. Then you will make a struct that has one variable with :2. So it will be a struct which has a twoBit variable. Then you will pass the smallInt.twoBit int to the function and you will have what you want. You won't do though case (00): You will do normally case (0), case(1), case(2), case(3).
If you assign a value greater than 3 on a 2 bit int then only the 2 last digits will be assign, thus, since 4 = 100 you will have 00, thus your int will be 0.

Finally note that you might gain space like this, even though the compiler with default options will change the struct to a fixed size of bytes, you might not gain performance with using just one variable. But the logic is the same as having a 2 bit variable.
Excellent. Thanks m8!

6. Originally Posted by C_ntua
Finally note that you might gain space like this, even though the compiler with default options will change the struct to a fixed size of bytes, you might not gain performance with using just one variable. But the logic is the same as having a 2 bit variable.
Indeed, so if the reason is to save space, then perhaps it is misguided. If the reason is to restrict the range to 4 values, then perhaps the real reason for the range is that there are a specific number of say, events, that are acceptable, so perhaps an enum or a full blown class tht emulates an enum would be better.

7. And you can combine the enums with the | operator. So you can pass two or three options with one parameter. Like MouseMoveDown(...., option1 | option2 | option 3) and then use a mask (using the &) to get individual bits/options

Popular pages Recent additions