20.04.2013, 03:33
(
Last edited by Yashas; 20/04/2013 at 04:36 AM.
)
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
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.
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
| 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
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
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.
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))
Code:
(1 << (bit % sizeof(variable)))
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)));
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))); }
~ 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)))); }
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.