In this Tutorial I'm wanting to show you, how to use Operators Bitwise to shorten and improve your code.
To Understand this tutorial, you will at least need to understand the binary numeral system.
Bitwise Operators
Scar can use the following operators bitwise.
AND, OR, XOR, NOT, (shr, shl)
Simple Bitwise Operations
Bitwise Operators Use The Binary Form of your number.
Here's an example, showing you why "25 AND 13" is 9.
25 AND 13 = 9;
25 in binary is
11001
13 in Binary is
1101
With Bitwise Operations you take every digit and compare it.
1 is "true" and 0 is "false".
Here's an example with AND. AND means, we only get a true result if both digits are TRUE.
11001
AND 01101
01001
In scar it would look somewhat like this:
SCAR Code:
WriteLn(inttostr(25 AND 13));
Now you should know why 25 AND 13 is 9.
But do you also know why 11 OR 12 is 15 ?
OR Results True if One Digit is 1.
Here's the Math:
1011
OR 1100
1111 ( = 15 )
Here's an example with XOR
5 XOR 6 = 3;
XOR Results True when only one result is true, not when both are true and not when none are true:
101
XOR110
011
And last but not least NOT:
NOTis a little different to the previous shown operators, NOTjust switches every digit in a binary number.
Example: not 7 = 8
Because
7 -> 0111
not 0111
1000 -> 8
How and where to use Bitwise Operations
The CPU is faster when it gets a bitwise operation instead of an usual operation, especially divisions and multiplications can slow the cpu down.
Some examples:
Feel Free to post some more :].
SCAR Code:
function FinishedChopping:boolean;
begin
IF TreeChopped OR AxeBroken then
result := true;
end;
//>>>>> shortens to:
function FinishedChopping:boolean;
begin
result := TreeChopped OR AxeBroken;
end;
This function will result TRUE, when either TreeChopped is True or Axe Broken is true.
Here's another one, same pattern with AND.
SCAR Code:
function StopMining:boolean;
begin
if NoMoreRocks AND Timeout then
result := true;
end;
// >>>>> shortens to:
function StopMining:boolean;
begin
result := NoMoreRocks AND Timeout;
end;
More potential uses for bitwise operators
Let's say you have a game, we call it ... tetris ...
In tetris you do have 7 Blocks every block has its own rotation and behaviours.
You could now use a Binary To Safe All Of that Information in one Number:
ie:
1111 - Let's say the first digit is if the piece is rotable, the second is if it does rotate normally, and the third and fourth digit tell us how the piece rotates if it does not rotate normal.
You could now use Bitwise Operators to quickly find out all the information about that certain piece.
Rotatable ( 1000 )
SCAR Code:
If (piecestatus AND 8 = 8) then
WriteLn('Piece is rotatable');
Rotatable and Rotates Normal ( 1100)
SCAR Code:
If (piecestatus AND 12 = 12) then
WriteLn('Piece is rotatable and rotates normal');
And so on...
Conclusion
In the first Example the Advantage with Bitwise Operations was quite obvious, a shorter code.
In the second example its not that obvious, the advantage there would be the cpu speed, tetris may not have been the best example, but generally, when you have formulars, which are really time critical, its often a good Idea to use Bitwise Operators.
Feel Free to ask, if anything is unclear :].
~caused