Bitwiser Object

by Danny Worth

Description

The Bitwiser Object enables you to easy perform bitwise operations against any value. Bitwise operations are extremely useful. The chances are if you've ever looked into packing multiple values into a single integer or encrypt/decrypt data you would have come across bitwise operations.

So what are bitwise operations? Well first lets get into bits. A typical integer in a 32bit application will have 32 binary digits known as bits. These are 4 byte integers. There are also shorts (16bit/2 byte integers) and bytes/chars (8bit/1 byte integers) and even integers with more than 32bits such as 64bit, 128bit and so on. Each bit represents a value and they are totalled for the final value. So as you can see from just stated for every byte, 8 bits are used. Non-unicode characters (ASCII) utilise 1 byte to represent a character and unicode utilise 2 bytes to represent a character. So you can perform bitwise operations to encrypt a string of characters just as easy as you could to a value.

Let's take the letter 'A' for example:
A = 65 (0x41) in Binary: 01000001

As mentioned above the bits of a value represent a value and with the letter 'A' / value 65 2 flags are set. Windows based operating systems are little endian based values which means that we work right to left when working with binary. Apple Mac uses big endian which is left to right.

The first bit represents a value of 1. Then for every bit after that the value is doubled so 1, 2, 4, 8, 16, 32, 64, 128 and so on. When working in hexadecimal its a little easier to visualise 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 and so on. So looking at 'A' 01000001 the 2 bits set are 1 (0x01) and 64 (0x40) totalling 65 (0x41).

So now you have a little understanding of how bits make up values I'll move onto bitwise operations. Bitwise operations are just operations that affect the bits in one way or another. A brief overview of each operation:

OR: Merges the bits of the 2 values. eg 01000001 ('A') OR 01000010 ('B') would return 01000011 ('C')
XOR: Toggles the bits of a value where the bits of the 2 overlap. eg 01000001 XOR 01000010 would return 00000011
AND: Clears the bits of a value where the bits of the 2nd value are not set. eg 10101010 AND 00111100 would return 00101000.
NOT: Inverts the bits of value. eg 11110000 would return 00001111
SHIFT: Moves the bits along in either left or right directions but when the bits move outside of their bounds they are truncated and lost. eg 00110110 Shift right 2 would return 00001101 then if shifted left 2 it would return 00110100.
ROTATE: Also known as circular shift, rotate is similar to shift except when the bits move out of bounds they wrap round. eg 00110110 rotate right 3 would return 11000110

Using a mixture of the operations on an integer can completely change its value. You could apply a rotate right, XOR with a value and then NOT. It then would be a matter of reversing the process (of course rotating left instead of right) to get it back to its original value. This is a basic encryption method employed with many packets that are sent between game clients and servers to protect the packet data. You could even store 4 x 1 byte values in a single integer. The above examples covered just a single byte but a 32bit integer would look like this 00100110111111101000110110000111 (654216583) 4 x 1 byte values could be extracted from this 00100110 (38), 11111110 (254), 10001101 (141), 10000111 (135). Or 8 x 4 bit values. 4 bits allows you to store a value of 0-15.

Conditions

Flag Set

Arguments



Bit Flag: 1 Bit of 32bit value (0-31)
Value: Value to test

Details



Test if a bit within a value is set. eg Bit flag 5 set in 845623: 11001110011100110111 would be true.
Top

Expressions

OR

Arguments



First Value: First value to perform OR operation against
Value: Second value to perform OR operation with
Returns: Result integer

Details



There are 3 OR functions one for each 8/16/32bit.

Merges the bits of the 2 values. For example OR_32( "Bitwiser Object", 2546, 341996 ) would return 344062.
Top

XOR

Arguments



First Value: First value to perform XOR operation against
Value: Second value to perform XOR operation with
Returns: Result integer

Details



There are 3 XOR functions one for each 8/16/32bit.

Toggles the bits of a value where the bits of the 2 overlap. For example XOR_32( "Bitwiser Object", 2546, 341996 ) would return 343582.
Top

AND

Arguments



First Value: First value to perform AND operation against
Value: Second value to perform AND operation with
Returns: Result integer

Details



There are 3 AND functions one for each 8/16/32bit.

Clears the bits of a value where the bits of the 2nd value are not set. For example AND_32( "Bitwiser Object", 2546, 341996 ) would return 480.
Top

NOT

Arguments



Value: Value to perform NOT operation against
Returns: Result integer

Details



There are 3 NOT functions one for each 8/16/32bit.

Inverts the bits of value. For example NOT_32( "Bitwiser Object", 2546 ) would return -2547.
Top

Rotate Left

Arguments



Value: Value to perform rotate operation against
Rotate Amount: The number of bits to rotate
Returns: Result integer

Details



There are 3 Rotate Left functions one for each 8/16/32bit.

Also known as circular shift, rotate is similar to shift except when the bits move out of bounds they wrap round. For example ROL_32( "Bitwiser Object", 2546, 3 ) would return 20368.
Top

Rotate Right

Arguments



Value: Value to perform rotate operation against
Rotate Amount: The number of bits to rotate
Returns: Result integer

Details



There are 3 Rotate Right functions one for each 8/16/32bit.

Also known as circular shift, rotate is similar to shift except when the bits move out of bounds they wrap round. For example ROR_32( "Bitwiser Object", 2546, 3 ) would return 1073742142.
Top

Shift Left

Arguments



Value: Value to perform shift operation against
Shift Amount: The number of bits to shift
Returns: Result integer

Details



There are 3 Shift Left functions one for each 8/16/32bit.

Moves the bits along in left direction but when the bits move outside of their bounds they are truncated and lost. For example LSH_32( "Bitwiser Object", 2546, 3 ) would return 20368.
Top

Shift Right

Arguments



Value: Value to perform shift operation against
Shift Amount: The number of bits to shift
Returns: Result integer

Details



There are 3 Shift Right functions one for each 8/16/32bit.

Moves the bits along in right direction but when the bits move outside of their bounds they are truncated and lost. For example RSH_32( "Bitwiser Object", 2546, 3 ) would return 318.
Top

Set Bit Flag

Arguments



Value: Value to set bit on
Bit: The number of bit to set (0-31)
Returns: Result integer

Details



Sets the specified bit of a value specified in the Bit argument. For example to set the first bit of Global Value A. Set Global Value A to SetBitFlag( "Bitwiser Object", Global Value A, 0 ).
Top

Clear Bit Flag

Arguments



Value: Value to clear bit on
Bit: The number of bit to clear (0-31)
Returns: Result integer

Details



Clears the specified bit of a value specified in the Bit argument. For example to clear the first bit of Global Value A. Set Global Value A to ClearBitFlag( "Bitwiser Object", Global Value A, 0 ).
Top

Toggle Bit Flag

Arguments



Value: Value to toggle bit on
Bit: The number of bit to toggle (0-31)
Returns: Result integer

Details



Toggles the specified bit of a value specified in the Bit argument. For example to toggle the first bit of Global Value A. Set Global Value A to ToggleBitFlag( "Bitwiser Object", Global Value A, 0 ).
Top

Embed Value

Arguments



Value to embed to: Value to embed on
Value to embed: Value to embed
Bit offset: Offset in bits where to place the value
Bit length: Length in bits of the value
Returns: Result integer

Details



Embeds a value into another allowing you to store multiple values in 1 integer. For example to embed 2 values 25 and 15 that use 5 bits in Global Value A. Set Global Value A to EmbedValue( "Bitwiser Object", Global Value A, 15, 0, 5 ) then again Set Global Value A to EmbedValue( "Bitwiser Object", Global Value A, 25, 5, 5 ).
Top

Retrieve Embedded Value

Arguments



Value to retrieve from: Value to retrieve embedded from
Bit offset: Offset in bits where to retrieve the value
Bit length: Length in bits of the value
Returns: Result integer

Details



Retrieves an embedded value from a value. For example to retrieve 2 embedded values that use 5 bits from Global Value A. Set First Value to RetrieveEmbeddedValue( "Bitwiser Object", Global Value A, 0, 5 ) then again Set Value 2 to EmbedValue( "Bitwiser Object", Global Value A, 5, 5 ).
Top

Get Signed

Arguments



Value: Value to get signed from
Returns: Result integer

Details



There are 2 Get Signed functions one each for 8/16bit.

Values in Fusion are all 32bit signed even if you retrieve a 8 or 16 bit value. As a result when retrieving an 8 or 16 bit value it returns an equivilent value of an unsigned value (as it is below the limit of a signed 32bit value). Get signed converts a 8/16bit value to signed. For example Signed8( "Bitwiser Object", 255 ) would return -1.
Top