Bitwise Operations

Many people scripting for sphere will have some sort of programming experience. However not every one is familiar with this. I thought I’d make a short and easy tutorial about working with the HEXADECIMAL and BINARY systems.

First off, the names:

Binary: Latin for 2 possibilities: 0 and 1 in the case of computers.
Decimal: Latin for 10 ciphers: 0 1 2 3 4 5 6 7 8 9
Hexadecimal: Latin for 16 ciphers: 0 1 2 3 4 5 6 7 8 9 A B C D E F

So basically hexadecimal is the same as decimal but with 6 additional ciphers.

0A=10
0B=11
0C=12
0D=13
0E=14
0F=15
010=16
011=17
etc.

So in sphere: 01F would be 1*16 + 15 = 31 decimal. 023 = 2*16 + 3 = 35 decimal.

Now you don’t have to do this by heart. Windows comes with a calculator. Put this in scientific mode. In the top left you will see: HEX DEC OCT BIN. OCT stands for octal (Latin for 8) which is not normally used for computers. Now to Convert from HEX to DEC, click ‘HEX’ first. Type in the number and then hit ‘DEC’. The number should now be converted to decimal. The reverse also applies. You can also convert to binary the same way.

Binary for computers is based on the powers of the number 2.

2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128

The above sequence represents a BYTE. Which consists of 8 bits. Each bit contains 0/1. So if bit 3 is set the value of the byte is increased with 8 (2^3=8). If all bits are ‘1’, the total value of a byte would become 255 (1+2+4+8+16+32+64+128). So a byte can have any value between 0 and 255. The value you can have with 4 bits would be between 0 and 15, which is between 0 and 0f in HEX. 255 would then be 0FF (15*16 + 15 = 255). So HEX is very closely related to binary, which is why it was developed in the first place.

The final thing to remark here is that BIN notation uses the same precedence as the other systems: the bit with the highest value is put to the left. The one with the lowest value to the right:

1101 = 13 (8+4+1)

So far no bitwise operations, but I want to make sure that everyone has at least the very basics binary and hexadecimal systems.

Logical OR

The OR function compares the bits in two numbers and returns a ‘1’ on a bits location if either of the two numbers compared contains a ‘1’ on that location. This will sound a bit vague but it easier if shown:

1001
0011 |
---------
1011

working from left to right: the first bit is ‘1’ for the top number and ‘0’ for the bottom one. One of them is ‘1’ so the result is ‘1’. The second bit compared is ‘0’ for both numbers. Neither of them is ‘1’ so the result is ‘0’. The third bit is the same as for the first bit. The last bit is ‘1’ for both. Since at least one of them is ‘1’ the result is also ‘1’.

So why would you want to use OR in sphere? Well, Because with OR you can set one bit without disturbing any other bits in that byte. In passing I would like to add that sometimes you will find a sequence of bytes referenced to by as a ‘WORD’. The rules for words are the same as for bytes. On to an example:

Let’s set the ‘newbified’ attribute (04) on a key. You could do ‘ATTR=04’. However, if the key was supposed to be locked down, that will clear the ‘move never’ attribute on the key. So we could use the instruction ATTR=+04 to set the newbified attribute. That will work, but now imagine that the newbified attribute is already set. It would get cleared and the ‘move always’ attribute would be set. The safe method to add an attribute in this case would be ATTR=|04. The pipe ( | ) symbol is the sphere notation for OR.

Logical AND

The AND operation, again, compares two numbers. However, with AND the result will only contain a ‘1’ if both numbers contain a ‘1’ on that location. Example:

1001
0011 &
---------
0001

Only the last bit contains a ‘1’ at the same location. The AND operations can be used to selectively turn off bits. In this way it can be used in the exactly opposite way the OR operation is used. This process is also known as ‘masking’.

Logical NOT
NOT, is an operator which is very useful in IF statements.

The sphere presentation of NOT is ‘!’.

IF (A==B) // do this
if A=B IF !(A==B) // do this if A is NOT B

IF (A) //do something if A has any value but 0
If !(A) // do this if A has a value of 0

Logical INVERSE
The sphere presentation if INVERSE is ‘~’. This operation inverts all ‘1’ into ‘0’ and all ‘0’ into ‘1’..

Example :

1011
~1011 = 0100

This operator is usually used in combination with AND to make masks.

As I already mentioned AND can be used to selectively turn of bits. This works by having a ‘1’ on alllocations you don’t want to change and having a ‘0’ at the location you want to ‘turn off’

EXAMPLE:

1101
1110 &
----------
1100

with the inverse commando can you easily make the mask you want… imagine want to set bit 3 to 0 (hex value 04). The mask would be 11111111111111111111111111111011, which is bother to work with of course… However you can create this same value by using ~100. Or ~04 in HEX. The final line would be: value&~04.

Lets say we want to remove several bits. I’ll use hex values for this example:

We want to remove 04000, 0200 and 01. The final command would be

Value&~(04000|0200|01)

If you use a certain mask a lot it can be useful to actually calculate it once. The real benefit of using this method however, lies in the fact that it is independent of the length of the word… In sphere, for example, the MOREZ value is a single byte. So the mask for turning off the first bit would be 0fe. However, MOREX is a double byte (single word) and so the mask would have to become 0fffe. In both situations &~01 would accomplish the same task without having to worry about the amount of bytes in the value.

For those without previous experience with bit operations this will all sound a bit vague, but sphere uses these a LOT… Open up spheredefs.scp and you will see long list of 01… 02… 04… Memory objects, CAN, flags, attributes, all of them use bits to store info for sphere. Effective use of logical operators can make your life a lot easier.

Gr,

Belgar

A virtual paradise
www.virtual-paradise.org