I want to check using binary, ex. 00000101. Then i have red declared as Decimal 1, if it excists inside the binary stirng, i want it to print, red is used. Then it should check if the 2nd digit in the binary string is used, if so, print Blue is used. etc.

Only two of those comparisons will ever possibly evaluate to true: RED and GREEN. If you pass RED into the function, I'll bet it prints RED and GREEN. You need to reconsider your comparison strategy there (after you fix the accidental octal notation you used).

What you are doing (in binary):

Code:

0001 (red)
OR 0010 (blue)
----
0011 (green)

so 'mask' == 0011

0011 (mask)
AND 0011 (green)
----
0011

so (GREEN & mask) == 0011

What are you comparing it against....??

09-29-2008

matsp

Your binary values, when used in the context you are describing, needs to have unique bit combinations, such as:

Only two of those comparisons will ever possibly evaluate to true: RED and GREEN. If you pass RED into the function, I'll bet it prints RED and GREEN. You need to reconsider your comparison strategy there (after you fix the accidental octal notation you used).

What you are doing (in binary):

Code:

0001 (red)
OR 0010 (blue)
----
0011 (green)

so 'mask' == 0011

0011 (mask)
AND 0011 (green)
----
0011

so (GREEN & mask) == 0011

What are you comparing it against....??

Ahh i see what you mean. What i tried to accomplish was, when using your code:

Code:

PrintData(RED | BLUE);
This would return "0011"

0011 (flag)
& 0001 (red)
----
0001

or ((flag & RED) == 1)
To see if the red bit in the flag is set to 1

simliar with

0011 (flag)
& 0010 (blue)
----
0010

or ((flag & BLUE) == 1)
To see if the blue bit in the flag is set to 1

Problem is, it dosent solve what i am trying to accomplish.
It dosent matter to me if the decimal value is 3 or 5, i am only after if the bit in that location is turned on or off.

0101
what i want to check is, if bit "1" is set to 1, print Red is used. If bit "2" is set to 1, print Blue is used. If bit "3" is set to 1, print Green is used. If bit "4" is set to 1, print Yellow is used.

So from the above binary string i would get the following output:
Red is used.
Green is used.

That way, i can send in alot of constants to the program and i only have to have one parameter that accepts it. So instead of writing a function:

void func1 (bool RED, bool BLUE, bool GREEN etc);

i can write:

void func1 (char colorstouse);

Then just call it with the flags that i need:

func1(RED | BLUE | GREEN | YELLOW);

instead of

func1(true, false, true, false, false etc);

09-29-2008

tabstop

Quote:

Originally Posted by piffo

Problem is, it dosent solve what i am trying to accomplish.
It dosent matter to me if the decimal value is 3 or 5, i am only after if the bit in that location is turned on or off.

0101
what i want to check is, if bit "1" is set to 1, print Red is used. If bit "2" is set to 1, print Blue is used. If bit "3" is set to 1, print Green is used. If bit "4" is set to 1, print Yellow is used.

So from the above binary string i would get the following output:
Red is used.
Green is used.

That way, i can send in alot of constants to the program and i only have to have one parameter that accepts it. So instead of writing a function:

void func1 (bool RED, bool BLUE, bool GREEN etc);

i can write:

void func1 (char colorstouse);

Then just call it with the flags that i need:

func1(RED | BLUE | GREEN | YELLOW);

instead of

func1(true, false, true, false, false etc);

You can't test that the result of the & equals one, since it might equal two, or four, or eight, or whichever bit you're looking at. You just need to test that it's nonzero: if (colorstouse & RED). And if you plan to do that, you need to make sure that there are no overlaps in the bits, which is why you use powers-of-two.

09-29-2008

Elysia

You are trying to use the individual bits of a parameter to determine what the information passed is.
But you must also know then, that we're working on a binary level, not octal or decimal or hexdecimal.
You should know that the binary OR operator adds together bits and that the binary AND operator selects out bits.
For example: 0000 1111 | 1111 0000 = 1111 1111
0000 1111 & 0000 1000 = 0000 1000
0000 1111 & 0001 0000 = 0000 0000