|
Cypher Olsen
Registered User
Join date: 7 Aug 2004
Posts: 54
|
04-27-2006 23:13
Hey Everyone. I'm having a little problem with bitwise math operators. Not working quite like I hoped. I understand most of the basics behind them, I'm just a little rusty using them. Here is what I'm trying to do. integer Part1 = 1; //00000001 integer Part2 = 2; //00000010 integer Part3 = 4; //00000100 integer Part4 = 8; //00001000 integer Part5 = 16; //00010000 integer Part6 = 32; //00100000 integer Part7 = 64; //01000000 integer Part8 = 128; //10000000
integer Whole;
//Now how do I set Whole to contain certain parts. Or set it to not have certain parts.
//I think this will SET a bit. Whole = Whole | Part5; //Whole now is made up of part5? Whole = Whole | Part6; //Whole now is made up of part5 and part6? right?
//What if I want to set multiple parts at once? Whole = Whole | Part2 | Part3 | Part4; //Does that work?
//Now.. to UNSET a bit... Whole = Whole & ~Part2; //Whole now DOESN'T include Part2. I'm pretty sure this works.
//Can I do these ALL at once? Whole = 0; Whole = Whole | Part1 | Part2 & ~Part3 | Part4 & ~Part5 & ~Part6 | Part7 | Part; //This DOESN'T seem to work...
So... what CAN and CAN'T I do to turn flags on/off in a bitfield? How do I do multiples in one command? Thanks a TON if you can help me out!
|
|
Jigsaw Partridge
A man of parts
Join date: 3 Apr 2005
Posts: 69
|
04-27-2006 23:51
I would try using brackets in your final expression, to make sure that the operations are carried out in the order you intend. There is a 'precedence' associated with bitwise operators, although one should/could memorise this, it is usually easier (and far more readable) to use brackets, and thereby force sub-expression evaluation. Can't see anything wrong with the statements previous to that, btw. You could always stick in a few llOwnerSay()'s to verify what's happening to your variable at each step.
|
|
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
|
04-28-2006 01:02
I think whole = whole & part5 is the operator to return ONLY part 5, not |.
Consider 11101111 | 10000 - you're ORing them so it reads (in 32 bits of course) 1 | 0 = 1 in ALL the places giving you 11111111.
11101111 & 10000 reads 1 & 0 =0 in all the places - giving you the correct value of the 5th column from the right. 11111111 & 10000 gives 0 everywhere except column 5 where 1 & 1 = 1.
|
|
Cypher Olsen
Registered User
Join date: 7 Aug 2004
Posts: 54
|
04-28-2006 02:16
When i did Whole = Whole | part5 I was doing just what I wanted. I was turning ON the bit for Part5. Whole = 0000 0000; Part5 = 0001 0000
// 0000 0000 // OR // 0001 0000 //======== // 0001 0000
I wasn't trying to return the value of Part5, but activate that bit in the bitfield. Thanks for replying though Eloise. and Jigsaw, you were absolutely correct. What I was doing was almost just right, i Just had an order of operations problem. Adding a few parenthesis in fixed it right up. It's working PERFECT!
|
|
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
|
04-28-2006 11:47
Ordinarily, I would suggest that, if you are just updating bit fields, build your masks and data fields, then combine them, like so: // If you are doing an update of data containing only Part1 through Part5:
Mask = Part1 | Part2 | Part3 | Part4 | Part5; // Mask = 0x1F; // using hexadecimal notation works, too
UpdatedData = Part1 | Part4; // UpdatedData = Part1 + Part4; // Addition works as well as the bitwise // OR operator, as long as you don't specify the same bit value twice
NewData = OldData & ~Mask | UpdatedData;
Masking comes in very handy in many instances of bitwise data field manipulation. 
|
|
Cypher Olsen
Registered User
Join date: 7 Aug 2004
Posts: 54
|
04-28-2006 18:51
Thanks for the additional syntax Talarus ^^
|