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

help with an algorithm

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?
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)


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.
thanks, that's exactly what i was looking for Smile

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

thanks again
It's the same algorithm you would implement using binary...4 bits will each represent the four sides...Smile
i kind of figured that Wink
just wasn't familiar with the "&" operator in this way
just curious, why?
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;)
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. Smile
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... Very Happy)
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
Related topics
can i help?
Help me with MySQL Account Maintenance and phpBB 2.0.15 :((
help, change pass my acc in cpanel
help me upgrade php to 5.
Need help adding a domain
help add admin in phpbb forum
How to solve this problem? Thanks
Algorithm help..
An algorithm to solve sudoku
I need some help with my algorithm
Google's New Anti-Spam Algorithm Deals With Content Farms
Determining the pathfinding algorithm being used?
Reply to topic    Frihost Forum Index -> Computers -> Software

© 2005-2011 Frihost, forums powered by phpBB.