Bitwise Operations

From SphereWiki
Revision as of 22:28, 28 March 2013 by RanXerox (talk | contribs) (Converting from decimal to binary and from binary to decimal)
Jump to: navigation, search

Bitwise operations are used to perform an action on the bits (the 1's and 0's) of a number. Understanding what bitwise operators do requires you to understand how to convert from decimal to binary and from binary to decimal.

In this article a "binary" formatted number (which represents the "bits" of a number) will be written with a 2... and a decimal number with a 10

Converting from decimal to binary and from binary to decimal

For example 110100112 is equal to 2111010, and 1431010 is equal to 100011112...

From decimal to binary

To convert the decimal number 783 to binary, you could think of it this way... divide the number by 2, over and over again and stop dividing if the quotient is 0. Here is a table that shows this concept:

Division: 783 / 2 391 / 2 195 / 2 97 / 2 48 / 2 24 / 2 12 / 2 6 / 2 3 / 2 1 / 2
Quotient: 391 195 97 48 24 12 6 3 1 0
Remainder: 1 1 1 1 0 0 0 0 1 1

...Now, read the sequence of remainders from right to left, to arrive at the binary number 1100001111.

Negative decimal number to binary

For example: -783

  1. Take the binary form of 783: 0000001100001111
  2. Invert it: 1111110011110000
  3. Then add 1 to it
  4. So, -78310 is 11111100111100012

The convention for storing negative numbers is typically to use the first "bit" to represent the negative. If the first bit is 0, the number is positive, if the first bit is 1, the number is negative. This is called a "signed integer", because the first bit indicates the sign.

How you can be sure that a binary number is signed or not? It depends entirely on the code. So, 11111100111100012 is equal to -78310 if you consider it to be signed... but if you consider it an unsigned number, since the first bit does not indicate the number is negative, 11111100111100012 is equal to 6475210.

In Sphere objects, an example of a 16 bit integer would be MORE1 or MORE2... as to whether it's value is interpreted as signed or not is up to the system or script that is using it. For example, the MORE1 and MORE2 on a Magery spellbook (an object with TYPE=t_spellbook), the value in MORE1 and MORE2 is an unsigned integer. And each bit in that number corresponds to a spell... MORE1 holds the first 32 spells (circles 1-4) and MORE2 is the last 32 spells (circles 5-8).

From binary to decimal

If you have a signed binary number 00000001000101102 (notice the first bit is 0, therefore it is a positive number), then reverse the order of the bits (giving you 01101000100000002), and use this method:

Bit b: 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0
b * 2n 0 * 20 1 * 21 1 * 22 0 * 23 1 * 24 0 * 25 0 * 26 0 * 27 1 * 28 0 * 29 0 * 210 0 * 211 0 * 212 0 * 213 0 * 214 0 * 215
Result: 0 2 4 0 16 0 0 0 256 0 0 0 0 0 0 0

Now, you have to add up all results: 0 + 2 + 4 + 16 + 0 + 0 + 0 + 256 + 0 + 0 + 0 + 0 + 0 + 0 + 0 = 2 + 4 + 16 + 256 = 278

Negative binary number to decimal

For example, 11111111110100112

  1. Invert the binary number: (11111111110100112 becomes 00000000001011002)
  2. Convert it to a decimal number: 4410
  3. Add 1: 45
  4. Negate it: -45
  5. Therefore, the negative binary number 11111111110100112 is equal to the decimal number -4510

The OR Operator (inclusive OR)

The AND Operator

The XOR Operator (exclusive OR)

The NOT Operator

==Left and right shift==