[Tutorial] Artificial Gouped Bool
#1

Artificial Gouped Bool

What is this??
Two functions that will set and unset each and every single bit of any variable.

Purpose of this tutorial??
You can group bools in one int(You can use Float too and get a bigger size).You can avoid using arrays of bools.Calculating arrays take longer time than bit-wise operators.

How to??
There will be 3 functions, one to set,unset,test a bit.All these ~ & | ^ << >> are bitwise operators.

Setting a bit
Code:
stock set_bit (&variable,new bit) 
{
  variable |= (1 << (bit % sizeof(variable)));
}
Code:
(bit % sizeof(variable))
This is done to ensure that no one will try to play with the computer.It does not allow a bit to be set that does not exists.

Code:
(1 << (bit % sizeof(variable)))
This does a left shift on the bit number.

Left Shift Operator
When the Left Shift is performed on data or a value ,the bits of the data or the value is shifted to the left.
Example:
000 110 << 1 -Operand left shifted by 1
001 100 -Result

000 011<< 1 -Operand left shifted by 1
000 110 -Result

000 001<< 1 -Operand left shifted by 1
000 010 -Result

Code:
variable |= (1 << (bit % sizeof(variable)));
| is known as bitwise OR operator.

The current value of the variable is ORed with the value given by (1 << (bit % sizeof(variable)))

Bitwise OR Operator:
This is similar to Bitwise AND but a tiny difference.Here all bits change to 1 if 1 is present in any of the operands.Only when the both the corrosponding bits are 0 they result in 0.This bitwise operator is denoted by the symbol "|".Just a single one not two.Two would make up to Logical OR("||").

Principle:
1 OR 1 = 1
1 OR 0 = 1
0 OR 1 = 1
0 OR 0 = 0


Example:
Result = Operand_1 | Operand_2;
1111001001110100 -Operand_1
0011010101100101 -Operand_2
----------------------
1111011101110101 -Result after ORing Operand 1 and 2.


Unsetting a bit
Code:
stock unset_bit_int (&variable,int bit) 
{ 
  variable &= ~ (1 << (bit % sizeof(variable)));
}
The R.H.S is already told.

~ is the unary ones complement operator
It flips all the bits of the variable.
Example:
Result = ~Operand_1;
1000100110101001 -Operand_1
0111011001010110 -Result after ones complimenting Operand_1
Testing a bit
Code:
stock test_bit (&variable,new bit) 
{ 
 return (variable &  (1 << (bit % sizeof(variable))));
}
The R.H.S skipped once again.
This time its the AND operator &.

Bitwise AND Operator
When two values are ANDed in C are compared bit by bit.There needs to be two operands in AND operator.The BItwise AND operator is denoted by the symbol "&" in C.When the two operands given to an AND both contain 1's in their corresponding bit position, the final result will be 1, rest all give out 0.You can understand easily in the example:

Principle:
0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1

Example:
Result = Operand_1 & Operand_2;

1111001001110100 -Operand_1
0011010101100101 -Operand_2
----------------------
0011000001100100 -Result after ANDing Operand 1 and 2.
BUGS:
None

Notes:
I wrote crap!Will change the whole content once again.
Reply
#2

Not bad
Reply
#3

int bit should actually be bool bit.

Try talking about bit arrays too.
Reply
#4

whats the point of left shifting a bit?
i know what it does but how is it useful
Reply
#5

Quote:
Originally Posted by mastermax7777
View Post
whats the point of left shifting a bit?
i know what it does but how is it useful
If you left bit shift a number, you multiply it by a power of 2.
E.g. a << b it's equal to a * 2^b

If you right bit shift a number, you divide it by a power of 2.
E.g. a >> b it's equal to a / 2^b

Read these articles for more:
http://www.gamedev.net/page/resource...ons-in-c-r1563
http://lab.polygonal.de/?p=81
Reply
#6

Quote:
Originally Posted by Dan..
View Post
If you left bit shift a number, you multiply it by a power of 2.
E.g. a << b it's equal to a * 2^b

If you right bit shift a number, you divide it by a power of 2.
E.g. a >> b it's equal to a / 2^b

Read these articles for more:
http://www.gamedev.net/page/resource...ons-in-c-r1563
http://lab.polygonal.de/?p=81
thanks.. will do
Reply
#7

Some people does not understand the idea of manipulating and checking bits inside 32 bit variables.
You can use the macros below to manipulate and/or check for bits inside 32 bit variables.

pawn Code:
// Sets a single bit at a special position (0-31)
// Usage: set_bit(variable, position)
#define set_bit(%0,%1)  %0 |= 1<<(%1)

// Sets multiple bits related to the second argument
// Usage: set_bits(variable, value)
#define set_bits(%0,%1) %0 |= (%1)

// Unsets a single bit at a special position (0-31)
// Usage: unset_bit(variable, position)
#define unset_bit(%0,%1)    %0 &= ~(1<<(%1))

// Unsets multiple bits related to the second argument
// Usage: unset_bits(variable, value)
#define unset_bits(%0,%1)   %0 &= ~(%1)

// Toggles a single bit at a special position (0-31)
// Usage: toggle_bit(variable, position)
#define toggle_bit(%0,%1)   %0 ^= 1<<(%1)

// Toggles multiple bits related to the second argument
// Usage: toggle_bits(variable, value)
#define toggle_bits(%0,%1)  %0 ^= (%1)

// Returns true, if the bit at a special position (0-31) is enabled otherwise false
// Usage: is_bit(value, position)
#define is_bit(%0,%1)   (!!((%0)&(1<<(%1))))

// Returns true, if ONLY the bit at a special position (0-31) is enabled otherwise false
// Usage: is_only_bit(value, position)
#define is_only_bit(%0,%1)  ((%0) == (1<<(%1)))

// Returns true, if the bits related to the second argument are enabled otherwise false
// Usage: are_bits(value, value_2)
#define are_bits(%0,%1)     (((%0)&(%1)) == (%1))

// Returns true, if ONLY the bits related to the second argument are enabled otherwise false
// Usage: are_only_bits(value, value_2)
#define are_only_bits(%0,%1)    ((%0) == (%1))

// Returns true, if the bit at a special position (0-31) is disabled otherwise false
// Usage: isnot_bit(value, position)
#define isnot_bit(%0,%1)    (!((%0)&(1<<(%1))))

// Returns true, if the bits related to the second argument are disabled otherwise false
// Usage: arenot_bits(value, value_2)
#define arenot_bits(%0,%1)  (((%0)&(%1)) == 0)

// Returns true, if ONLY the bits related to the second argument are disabled otherwise false
// Usage: arenot_only_bits(value, value_2)
#define arenot_only_bits(%0,%1) ((~(%0)) == (%1))
Reply
#8

OMG I got pretty much jacked!!
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)