c++ multiple enums in one function argument using bitwise or "|" - c++

I recently came across some functions where you can pass multiple enums like this:
myFunction(One | Two);
Since I think this is a really elegant way I tried to implement something like that myself:
void myFunction(int _a){
switch(_a){
case One:
cout<<"!!!!"<<endl;
break;
case Two:
cout<<"?????"<<endl;
break;
}
}
now if I try to call the function with One | Two, I want that both switch cases get called. I am not really good with binary operators so I dont really know what to do. Any ideas would be great!
Thanks!

For that you have to make enums like :
enum STATE {
STATE_A = 1,
STATE_B = 2,
STATE_C = 4
};
i.e. enum element value should be in power of 2 to select valid case or if statement.
So when you do like:
void foo( int state) {
if ( state & STATE_A ) {
// do something
}
if ( state & STATE_B ) {
// do something
}
if ( state & STATE_C ) {
// do something
}
}
int main() {
foo( STATE_A | STATE_B | STATE_C);
}

Bitwise operators behave well only with powers of 2:
0010
| 0100
------
0110 // both bits are set
0110
& 0100
------
0100 // nonzero, i.e. true: the flag is set
If you try to do the same with arbitrary numbers, you'll get unexpected results:
0101 // 5
| 1100 // 12
------
1101 // 13
Which contains the possible (arbitrary) numbers as set flags: 0001 (1), 0100 (4), 0101 (5), 1000 (8), 1001 (9), 1100 (12), 1101 (13)
So instead of giving two options, you just gave six.

Usually arguments that are combined that way are flags (a value with a single bit set) with a decimal value of 1, 2, 4, 8, etc. Assuming that One and Two follow this rule, you cannot use a switch to check for both. Switches only follow one path. Your combined argument does not equal One or Two, but a combination of them (1 | 2 == 3). You can check to see if One or Two is set like this:
if (_a & One)
{
}
if (_a & Two)
{
}
Remember that a standard enum without explicit values will just count upwards, not use the next bit. If your next defined value is Three, it will likely equal 3 which is not a value of a single bit, and will then act as if you had passed both flags (One | Two) to the function. You'll need to set the values of the enum yourself.

You must split the possible "tokens" (non-overlapping of course... use power of 2):
if (_a & One) { ... }
Not elegant really do what you want with 1 switch statement: split using if statements.

You are better off doing it with a set of if statements ...
ie
if ( _a & ONE )
{
// Do stuff.
}
if ( _a & TWO )
{
// Do other stuff.
}
edit: You could also do it in a switch statement but it would be a nightmare. Youd need something like this
switch( _a )
{
case ONE:
// Handle ONE.
break;
case TWO:
// Handle TWO.
break;
case ONE | TWO:
// Handle ONE.
// Handle TWO.
break;
};
Its relatively sane for only 2 options but once you get more than that it starts to balloon out of control. If you have 32-options you'd have a switch statement that would be unlikely to fit on any machine. All in the "if" solution is much cleaner and far more sane :)

Related

Bitwise operators and bit manipulation (no solution?)

I've been racking my brain trying to come up with a solution, but am stuck.
I have a function, this function takes the input (either 0b0, 0b1, or 0b10) and sets the bit of some variable - or at least it's supposed to.
The value it's changing has two states: either 0b100 or 0b101. I want to set the respective bit from the input. This is easy for the true bits, but tricky for the false bits. Some pseudo code for all the scenarios:
if (var == 0b100 && input == 0b0) { do nothing } // bit already set
if (var == 0b101 && input == 0b0) { var = 0b100 } // bit is different, so we set it.
if (var == 0b100 && input == 0b1) { var = 0b101 } // bit is different, so we set it.
if (var == 0b101 && input == 0b1) { do nothing } // bit is already set
if (var == 0b100 && input == 0b10) { var = 0b110 } // bit is never set, so set it
if (var == 0b101 && input == 0b10) { var = 0b110 } // bit is never set, so set it
I don't want to cheat and make a separate statement for every possibility, I'd like a function for this. Here's what I've sort of strung together, but it obviously doesn't work:
if ( var ^ input )
{
var ^= input;
}
The issue with this code is that 3rd bit (far left one) is always true.
Is this even possible?
EDIT:
Here is the same question, just posed a different way (which will result in different answers).
A function takes input for one bit. I want to take the left most bit and compare that bit and only that bit to another variable. e.g. input = 0b10 compared to 0b101's second bit, 0b00, since 0b10's left most bit is the second one.
On rare occasion, the unchanging bit may change :P (not from me though), so 0b001 or 0b000 may be it's value. For this reason, I cannot create a dynamic mask and compare only my bit.. The best method to solve my issue would be to answer this alternative question. Thanks a ton for your help, guys!
Step 1: Mask out the lower two bits.
var &= 0b100;
Step 2: Assign the lower two bits through OR operation.
var |= input;
Note that you may need to change the bit mask in step 1 in actual code base on the actual data type.

simple program without using "if condition"?

Is there any way of creating a simple java(or c,c ++, python) program that prints 3 (outputs the 3) when given input=6 and it gives output=6 when given input=3 without using "if conditions" ?
Assuming you're happy for it to produce other outputs on inputs that aren't 6 or 3, then you can just compute 9-x.
You can always just use a switch-case statement. Also, if you only want those two answers, you could also take the input as an int and do 9-[your int] and print that answer.
You can use the XOR bit operation. It compares pairs of bits and returns 0 if bits are equals and 1 if bits are different.
We have 3 = 011b and 6 = 110b. This numbers differ by 1 and 3 digit (bit), so XOR mask will be 101b = 5.
Code example:
public static int testMethod(int value){
return System.out.println(value ^ 5);
}
without if or without control flow statement/condition statement ?
you could use switch statement
private void tes(int i) {
switch (i) {
///give output 6 where input is 3
case 3:
System.out.println(6);
break;
///give output 3 where input is 6
case 6:
System.out.println(3);
break;
}
}

What are flags and bitfields?

Can someone explain to me what flags and bitfields are. They seems to be related to each other, or mabye i got the wrong idea. I kinda grasp bits and pieces of what they do and are but I would like to get them fully explain and I can't really find any good tutorials or guides.
I would be really thankful if someone could give some good examples on how to use them etc... For instance i see these kinds of expressions all the time and I dont fully understand them. Just that they are some kind of logical operators or something
VARIABLE1 | VARIABLE2
Thanks in advance!
An introduction to bitwise operations can be found here: http://www.codeproject.com/Articles/2247/An-introduction-to-bitwise-operators
As they apply to flags, bitwise operations are advantageous in that they are very fast and save on space. You can store many different states of an object within a single variable by using mutually exclusive bits. i.e.
0001 // property 1 (== 1, 0x01)
0010 // property 2 (== 2, 0x02)
0100 // property 3 (== 4, 0x04)
1000 // property 4 (== 8, 0x08)
These can represent four different properties of an object (these are the "masks"). We can add a property to an object's flags state by using or:
short objState = 0; // initialize to 0
objState |= 0010;
This adds property 2 above to objState by "or"-ing 0010 with 0000, resulting in 0010. If we add another flag/property like so:
objState |= 0100;
we end up with objState = 0110.
Now we can check if the object has the flag for property 2 set, for example, by using and:
if (objState & 0010) // do something
and is 1 if and only if both bits are 1, so if bit 2 is 1, the above operation is guaranteed to be non-zero.
So as I mentioned, the advantages of this way of handling properties/flags of an object is both speed and efficiency. Think of it this way: you can store a set of properties in a single variable using this method.
Let's say, for example, you have a file type and you wish to keep track of the properties using bit masks (I'll use audio files). Maybe bits 0 - 3 can store the bit-depth of the file, bits 4 - 7 can store the file type (Wav, Aif, etc.), and so on. You then just need this one variable to pass around to different functions and can test using your defined bit-masks instead of having to keep track of potentially dozens of variables.
Hope that sheds some light on at least this one application of bitwise operation.
A "bifield" is a set one or more bits in a "word" (that is, say an int, long or char) that are stored together in one variable.
You can for example have "none, "spots" and/or "stripes" on some animal, and it can also have "none, short, medium or long" tail.
So, we need two bits to reprent the length of a tail:
enum tail
{
tail_none = 0,
tail_short = 1,
tail_medium = 2,
tail_long = 3
};
We then store these as bits in the "attributes":
enum bits_shifts
{
tail_shift = 0, // Uses bits 0..1
spots_shift = 2, // Uses bit 2
stripes_shift = 3
};
enum bits_counts
{
tail_bits = 2, // Uses bits 0..1
spots_bits = 1, // Uses bit 2
stripes_bits = 1
};
We now pretend that we have fetched from some input the tail_size and the has_stripes, has_spots variables.
int attributes;
attributes = tail_length << tail_shift;
if (has_spots)
{
attributes |= 1 << spots_shift;
}
if (has_stripes)
{
attributes |= 1 << stripes_shift;
}
Later we want to figure what the attributes are:
switch((attributes >> tail_shift) & (1 << tail_bits)-1))
{
case tail_none:
cout << "no tail";
break;
case tail_short:
cout << "short tail";
break;
case tail_medium:
cout << "medium tail";
break;
case tail_short:
cout << "long tail";
break;
}
if (attributes & (1 << stripes_shift))
{
cout << "has stripes";
}
if (attributes & (1 << spots_shift))
{
cout << "has spots";
}
Now, we have stored all this in one integer, and then "fished it out" again.
You can of course do something like this too:
enum bitfields
{
has_widget1 = 1,
has_widget2 = 2,
has_widget3 = 4,
has_widget4 = 8,
has_widget5 = 16,
...
has_widget25 = 16777216,
...
}
int widgets = has_widget1 | has_widget5;
...
if (widgets & has_widget1)
{
...
}
It is really just an easy way to pack several things into one variable.
the bits of an integer value can be used as bools.
http://msdn.microsoft.com/en-us/library/yszfawxh(v=vs.80).aspx
Make with | and retrieve with &.
enum { ENHANCED_AUDIO = 1, BIG_SPEAKERS = 2, LONG_ANTENNA = 4};
foo(HAS_CAR | HAS_SHOE); // equiv to foo(3);
void processExtraFeatures(flags) {
BOOLEAN enhancedAudio = flags & ENHANCED_AUDIO; // true
BOOLEAN bigSpeakers = flags & BIG_SPEAKERS; // true
BOOLEAN longAntenna = flags & LONG_ANTENNA; // false
}
A "flag" is a notional object that can be set or not set, but not a part of the c++ language.
A bitfield is a language construct for using sets of bits that may not make up an addressable object. Fields of a single bit are one---often very good---way of implementing a flag.

What does this c++ statement check? (Box2d)

if ((catA & maskB) != 0 && (catB & maskA) != 0)
It is in Box2d's manual: 6.2, and is used to check if two objects should collide (after filtering)
It checks that catA has at least one common '1' bit with maskB, and catB has at least one common '1' bit with maskA.
For example, if catA is 3 (binary 00000011) and maskB is 10101010), then (catA & maskB) != 0 is true because catA & maskB is 00000010.
This is called masking, which means only keeping bits of interest.
You frequently have this kind of construct :
#define READ 1
#define WRITE 2
#define READWRITE (READ|WRITE)
#define DIRECTORY 4
int i=getFileInfo("myfile");
if(i & READWRITE)puts("you can read or write in myfile");
if(i & DIRECTORY)puts("myfile is a directory");
BTW, "i & DIRECTORY" means the same as "(i & DIRECTORY) != 0"
catA is a bit field of collision categories for object A
maskA is a bit field of categories that object A can collide with.
For example:
catA = 100010000010010 // Object A is in 4 collision categories
maskA = 001010000000000 // Object A can collide with too different categories
catB = 000010001000001 // Object B is in 3 collision categories
maskB = 100000000000000 // Object B can only collide with the category represented by the highest bit
catA & maskB means the bits that are 1 in both catA and maskB, so 1000000000000000. It's not 0, because Object B can collide with objects in the highest bit, and object A has that bit set.
catB & maskA means the bits that are 1 in both catB and maskA, so 0000100000000000. It's also not zero since Object A can collide with objects in the 5th highest bit category, and Object B is in that category.
So the two objects can collide.
the symbol & is the bitwise AND operator. Therefore, the statement
(catA & maskB) != 0
checks to see if any bits overlap in both of those items. It looks at if it checks A against B first, then B against A.
I know many C/C++ programmers love terseness but I find this type of code can be made much more readable by moving this test into a function (you can inline it if you want). They could also have gotten rid of the comment by moving the code to a well named method.
if (FixturesCanCollide() )
{
}
You can actually throw away the comparison to !=0 (although you might prefer it for clarity, either way it probably compiles down to the same code.
inline bool FixturesCanCollide()
{
return (catA & maskB) && (catB & maskA);
}

evaluate whether a number is integer power of 4

The following function is claimed to evaluate whether a number is integer power of 4. I do not quite understand how it works?
bool fn(unsigned int x)
{
if ( x == 0 ) return false;
if ( x & (x - 1) ) return false;
return x & 0x55555555;
}
The first condition rules out 0, which is obviously not a power of 4 but would incorrectly pass the following two tests. (EDIT: No, it wouldn't, as pointed out. The first test is redundant.)
The next one is a nice trick: It returns true if and only if the number is a power of 2. A power of two is characterized by having only one bit set. A number with one bit set minus one results in a number with all bits previous to that bit being set (i.e. 0x1000 minus one is 0x0111). AND those two numbers, and you get 0. In any other case (i.e. not power of 2), there will be at least one bit that overlaps.
So at this point, we know it's a power of 2.
x & 0x55555555 returns non-zero (=true) if any even bit it set (bit 0, bit 2, bit 4, bit 6, etc). That means it's power of 4. (i.e. 2 doesn't pass, but 4 passes, 8 doesn't pass, 16 passes, etc).
Every power of 4 must be in the form of 1 followed by an even number of zeros (binary representation): 100...00:
100 = 4
10000 = 16
1000000 = 64
The 1st test ("if") is obvious.
When subtracting 1 from a number of the form XY100...00 you get XY011...11. So, the 2nd test checks whether there is more than one "1" bit in the number (XY in this example).
The last test checks whether this single "1" is in the correct position, i.e, bit #2,4,6 etc. If it is not, the masking (&) will return a nonzero result.
Below solution works for 2,4,16 power of checking.
public static boolean isPowerOf(int a, int b)
{
while(b!=0 && (a^b)!=0)
{
b = b << 1;
}
return (b!=0)?true:false;
}
isPowerOf(4,2) > true
isPowerOf(8,2) > true
isPowerOf(8,3) > false
isPowerOf(16,4) > true
var isPowerOfFour = function (n) {
let x = Math.log(n) / Math.log(4)
if (Number.isInteger(x)) {
return true;
}
else {
return false
}
};
isPowerOfFour(4) ->true
isPowerOfFour(1) ->true
isPowerOfFour(5) ->false