You are invited to Log in or Register a free Frihost Account!

# help with an algorithm

elcyron
hi there,

i'm new to programming so this is probably pretty easy to solve but i just cant do it...

the idea is to save several booleans in a single integer.
image a sqare, 4 corners - each one might be rounded. i DONT want to declear an bool for every corner and set it to TRUE for rounded or FALSE for not rounded.

what i want is to assign values to each corner and add them to get a unique value for each possible combination of rounded and unrounded corners.

values for the corners would be 1, 2, 4, 8.
so a resulting value of 6 would have second and third corner rounded.

this IS pretty easy, after all i figured this out by my self. but how the hell can i implement that ?

one way might be to convert the value to binary and compare the digits.
another one could be a huge if-statement that decraeses until value=0.

i guess there is a real easy way i just can't come up with. probably something containing an ANDstatement.

can anyone give me a hint?
Naif
You can use a single character variable for that and use bitwise operators to figure out the values. You'll be able to hold 8 values in one char variable.

Let the low order bit represent the first corner, the second bit the second corner and so forth...

Assuming the 1st and 2nd corners are rounded where as the others arent, your byte value will look like this: 00000011, i.e. 3 in decimal.

Now to check which corners are rounded or not, you can do this

(Assuming we use a variable named truthvalue)

 Code: if(truthvalue & 1) printf("First corner is rounded"); if(truthvalue & 2) printf("Second corner is rounded"); if(truthvalue & 4) printf("Third corner is rounded"); if(truthvalue & 8) printf("Fourth corner is rounded");

and so on.... Note that the "&" operator used is a bitwise AND and not a logical AND. If the value of the variable truthvalue is 13, i.e. 1101 in binary, all the statements except for the second corner will evaluate to true.
elcyron
thanks, that's exactly what i was looking for

wasn't sure it would compile, as i thought the & would be interpreted as a reference. but it compiled and worked perfectly.

thanks again
leodv
It's the same algorithm you would implement using binary...4 bits will each represent the four sides...
elcyron
i kind of figured that
just wasn't familiar with the "&" operator in this way
blu3bird
just curious, why?
elcyron
in c++ the & marks a reference to a pointer or variable. that's the only way i ever used it. i probably just forgot the chapter on bitwise comparison, or never read it in the first place;)
djclue917
 elcyron wrote: in c++ the & marks a reference to a pointer or variable. that's the only way i ever used it. i probably just forgot the chapter on bitwise comparison, or never read it in the first place;)

Yes you are so right.
In C++, '&' is the address-of operator when used like this: *foo = &bar;
And a bitwise operator when used like this: *foo++ &= bar;

Correct me if I'm wrong... I haven't programmed in C++ for nearly a year now.. (Busy with studies... )
elcyron
i mostly use the & for calling by reference.
like this: foo( int& bar)
or: foo( const int& bar)
so when i call foo the submitted value is used, rather then a copy of that value