You want to look up bitwise operators, and use hexadecimal. These aren't covered in the BGT manual.
Each hex digit represents 4 bits, and since there are only 16 (0-f), it's fairly easy to remember (or calculate) what binary sequence they represent.
The bitwise operators are hard to explain in a forum post. I'll try to summarize as best I can:
& (and): for single bits x and y, x&y==1 only when x==1 and y==1. This is the most useful way to find the value of any particular bit. For example:
if((x&1)==0) alert("Even number", x + " is even!");
(OK, you can do the same as the example with x%2==0, but that's beside the point. If you need to check for the 4, 8, or 32 bits, or a collection of bits, this is how you'd do it.)
| (or): x|y==1 if x==1 or y==1. This is most useful for setting desired bits to 1, even if you don't know their original state. For example,
x=y|5;
Means that, no matter what the value of x, the 4 and 1 bits will be set.
^ (exclusive or, xor): x^y==1 if only one of x or y is 1, but not both. This is most useful if you want to toggle a bit. For example:
x^=1;
Will set the 1 bit if x is even, or set it to 0 if x is odd.
(FYI: you can use the (logical) xor keyword in conditionals. I didn't know that until very recently... what a typing-saver that would have been, even though it doesn't come up often.)
~ (not): this is a unary operator that flips all the bits in a byte/bytes. Try it on a bunch of random numbers, and see the twos-complement algorithm in action. (Which is to say, for a signed integer, ~0==-1. For an unsigned integer, ~0=the maximum value for the bytesize.) This is mostly useful because you're manipulating individual bits on the byte level, and this is how you'd set specific bits to 0 without changing the others. For example:
x=x&~4;
Sets the 4 bit to 0, without changing the other bits.
<< and >> (left and right shift) move all the bits to the left/right (where lower-value bits are to the right, and higher-value bits to the left). This is equivalent to integer-multiplication (<<) or division (>>) by powers of 2, but is useful when you want to store multiple values in a single int, which you need to work with as lower-order values later.
For example, say you have 4 integer values (a, b, c, d) that you want to save in the smallest space possible. If you can expect them to only have a maximum of 16 distinct states, you could store them like so:
int16 bytes=a|(b<<4)|(c<<8)|(d<<12);
And to retrieve them:
a=bytes&0xf;
b=(bytes>>4)&0xf;
c=(bytes>>8)&0xf;
d=(bytes>>12)&0xf;
For all of these, you can use combined read/write operations, for example x|=y, x<<=y, x&=~y, x^=y.
Usually, I use bitwise stuff when I need to use an arbitrary number of boolean properties, because it turned out to be much easier and more efficient (for me, anyway) than using tons of bools. Especially since I almost never need more than 32, usually need more than 8, and can usually predict based on the program whether or not 16 will be enough. It makes it possible to leave room for new flags without having to change much.
看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
George... Don't do that.