Jump to content
Sign in to follow this  
Doolittle

Bitwise operators?

Recommended Posts

Anyone know how to simulate flags like the bitwise operators you have in C??

I want to make A = 1, B = 2, C = 4... so if I have a 3 then I have A and B.

Argh.

Share this post


Link to post
Share on other sites

Maybe use a boolean array to simulate it. Probably easier to work with too and you probably wouldn't need converters then.

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">[true, false, true, true, false] to represent 10110

If still needed, you could then write a loop to calculate the decimal value based on the position of each boolean field value.

Untested. Something like:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">_val = 0;

_pos = count _array;

{

_pos = _pos-1;

_val = _val+(2^_pos);

} forEach _array;

Share this post


Link to post
Share on other sites

I'm just curious, what is the practical problem you are trying to solve with this?  huh.gif

Or are you just being a 'scientist'.

Share this post


Link to post
Share on other sites

I'd like to do stuff like this:

<table border="0" align="center" width="95%" cellpadding="0" cellspacing="0"><tr><td>Code Sample </td></tr><tr><td id="CODE">// the flag definitions

#define CAR_LOCKED 0x01 // 0000 0001

#define CAR_PARKED 0x02 // 0000 0010

#define CAR_RESET 0x00 // 0000 0000

// macros to manipulate the flags

#define RESET_CAR(x) (x = CAR_RESET)

#define SET_LOCKED(x) (x |= CAR_LOCKED)

#define SET_PARKED(x) (x |= CAR_PARKED)

#define UNSET_LOCKED(x) (x &= (~CAR_LOCKED))

#define UNSET_PARKED(x) (x &= (~CAR_PARKED))

#define TOGGLE_LOCKED(x) (x ^= CAR_LOCKED)

#define TOGGLE_PARKED(x) (x ^= CAR_PARKED)

// these evaluate to non-zero if the flag is set

#define IS_LOCKED(x) (x & CAR_LOCKED)

#define IS_PARKED(x) (x & CAR_PARKED)

http://www.somacon.com/p125.php

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
Sign in to follow this  

×