## LSL Wiki : bitfield

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are crawl427.us.archive.org

## Bitfield

A bitfield is a method for storing a lot of yes/no, set/not_set, or on/off data in one integer. Because an integer is made up of 32 1s and 0s (bits), each of the 32 positions in an integer can be made to represent something. Essentially, it is a way of looking at an integer on the bit level where each bit is yes/no or on/off flag. Each of these bits in a bitfield is then a bitflag. When used with bitwise operators the results can be very powerful.

For example, if we want to describe fruit we might have a fruit bitfield. We could decide that the first bit (furthest right) is if it is round or not, the second bit is whether it is red or not and the third bit is whether it grows on a vine. Thus, an apple could be 011 or 3 (round, red and not grown on a vine), a grape could be 111 or 7 (round, red and grown on a vine), and a banana could be 000 or 0 (not round, not red and not grown on a vine). Each position is a power of 2 (remember 2^0 is 1).

If we wanted to see if a given fruit bitfield represented a round fruit we could use the following:
`if (myFruit & 1)`

To make it more legible we may define a set of global integers:
```integer ROUND = 1; // 0001 binary
integer RED   = 2; // 0010 binary
integer VINE  = 4; // 0100 binary```

Then the previous if can be stated in the very legible form:
`if (myFruit & ROUND)`

If we want to set a single field, or multiple fields, that is also easy:
`myFruit = myFruit | ROUND`

It's easier to work with bitfields with hex than decimal constants. (0x20000000 == 536870912)

(edit: s/then/than/ ; not sure if this is what was meant, but scanning this sentence 20 times it's the most likely one. I can see it be 'then', but that doesn't make much sense.)

Functions or events that use bitfields include: llSensor, llParticleSystem, llGetAgentInfo, control.

Boolean | Operators | Integer | Bitwise | Binary