### Monday, January 22, 2007

Use of bitwise operator in PHP

I though this time I should talk some thing about Bitwise

operator. Though bitwise operator is such a powerful tool,

we hardly use or talk about it.

The bitwise operator can be used for many reasons for

example remembering the bits that was set using a set of

options in a form.

Following are few bitwise methods in PHP that might help

others in working with bitwise operators:

//used to initialize

function init_bit( &$bitfield )

{

$bitfield = ($bitfield | 0);

}

/* Return true or false, depending on if the bit is set */

function query_bit( $bitfield, $bit )

{

return ( $bitfield & $bit );

}

/* Force a specific bit(pattern) to ON */

function set_bit( &$bitfield, $bit )

{

$bitfield |= $bit;

}

/* Force a specific bit(pattern) to be OFF */

function remove_bit( &$bitfield, $bit )

{

$bitfield &= ~$bit;

}

/* Toggle a bit(pattern), so bits that are on are turned

off, and bits that are off are turned on. */

function toggle_bit( &$bitfield, $bit )

{

$bitfield ^= $bit;

}

?>

Bitwise Operators can be very helpful in certain tasks

like, processing too many options set by the users in a

form or managing permission levels etc.

The general operations possible are:

1. AND &

2. OR |

3. XOR ^

4. NOT ~

5. ShiftLeft <<

6. ShiftRight >>

AND OPERATOR:

The "And" operator takes two values and returns a decimal

version of the binary values the left and right variable

share.

So for example if we have 2 and 10, we would have following

Binary Equivalent of 2 : 0010

Binary Equivalent of 10: 1010

So now if we do 2 & 10

we will have 0010 ie 2. So, the And operator returns the

bits that are set (i.e. set to “on”) in both parameters.

OR OPERATOR:

The "OR" operator would find the bits which are set in

either of the two parameters. So proceeding with the

previous example we would have the following

2 | 10

so we will have 1010 ie 10. So, the "OR" operator will

return the bits that are set (to “on”) in one or more

parameters.

XOR OPERATOR:

These are those operator which would return those bits

which are set in either of both the parameters but

excluding those which are common to both.

Proceeding with the example above, if we do

2 ^ 10

we will have 1000 so it yields 8.

NOT OPERATOR:

This operator would return those bits which are set in

first attribute but not set in the second attribute.

So if we have

2 & ~10

we will have 0.

and ~2 & 10

will return 8

SHIFT LEFT OPERATORS

This would make the bits shift on left specified by the

number of digits.

For example

2 << 2

would make

000010

001000

-------

so the out put would be 8. It is equivalent to multiplying

2 by 2 twice.

So we can conclude that

expr >> n = expr x 2**n.

SHIFT RIGHT OPERATORS

This would make the bits shift on right specified by the

number of digits.

So 2 >> 2

would make

000010

001000

------

000000

expr1 / 2**n.

so we have

2/2 **2 = 0

so the output would be 0

Hope you find it helpful. Do let me know, what you think

about it.

I though this time I should talk some thing about Bitwise

operator. Though bitwise operator is such a powerful tool,

we hardly use or talk about it.

The bitwise operator can be used for many reasons for

example remembering the bits that was set using a set of

options in a form.

Following are few bitwise methods in PHP that might help

others in working with bitwise operators:

//used to initialize

function init_bit( &$bitfield )

{

$bitfield = ($bitfield | 0);

}

/* Return true or false, depending on if the bit is set */

function query_bit( $bitfield, $bit )

{

return ( $bitfield & $bit );

}

/* Force a specific bit(pattern) to ON */

function set_bit( &$bitfield, $bit )

{

$bitfield |= $bit;

}

/* Force a specific bit(pattern) to be OFF */

function remove_bit( &$bitfield, $bit )

{

$bitfield &= ~$bit;

}

/* Toggle a bit(pattern), so bits that are on are turned

off, and bits that are off are turned on. */

function toggle_bit( &$bitfield, $bit )

{

$bitfield ^= $bit;

}

?>

Bitwise Operators can be very helpful in certain tasks

like, processing too many options set by the users in a

form or managing permission levels etc.

The general operations possible are:

1. AND &

2. OR |

3. XOR ^

4. NOT ~

5. ShiftLeft <<

6. ShiftRight >>

AND OPERATOR:

The "And" operator takes two values and returns a decimal

version of the binary values the left and right variable

share.

So for example if we have 2 and 10, we would have following

Binary Equivalent of 2 : 0010

Binary Equivalent of 10: 1010

So now if we do 2 & 10

we will have 0010 ie 2. So, the And operator returns the

bits that are set (i.e. set to “on”) in both parameters.

OR OPERATOR:

The "OR" operator would find the bits which are set in

either of the two parameters. So proceeding with the

previous example we would have the following

2 | 10

so we will have 1010 ie 10. So, the "OR" operator will

return the bits that are set (to “on”) in one or more

parameters.

XOR OPERATOR:

These are those operator which would return those bits

which are set in either of both the parameters but

excluding those which are common to both.

Proceeding with the example above, if we do

2 ^ 10

we will have 1000 so it yields 8.

NOT OPERATOR:

This operator would return those bits which are set in

first attribute but not set in the second attribute.

So if we have

2 & ~10

we will have 0.

and ~2 & 10

will return 8

SHIFT LEFT OPERATORS

This would make the bits shift on left specified by the

number of digits.

For example

2 << 2

would make

000010

001000

-------

so the out put would be 8. It is equivalent to multiplying

2 by 2 twice.

So we can conclude that

expr >> n = expr x 2**n.

SHIFT RIGHT OPERATORS

This would make the bits shift on right specified by the

number of digits.

So 2 >> 2

would make

000010

001000

------

000000

expr1 / 2**n.

so we have

2/2 **2 = 0

so the output would be 0

Hope you find it helpful. Do let me know, what you think

about it.