lua lubit_expl.lua
-------------------------------------------------------------------------------
B I T W I S E O P E R A T O R S (32-bit lua_Unsigned, auto conversion)
http://www.cs.cf.ac.uk/Dave/PERL/node36.html
-------------------------------------------------------------------------------
The bitwise operators are similar to the logical operators, except that
they work on a smaller scale -- binary representations of data.
& ... bitAND, | ... bitOR, ^^ ... bitXOR, ~ ... bitNOT
Note !! Both operands associated with the bitwise operator must be integers. !!
in lualubit there is automatic conversion
--------------------
RANGES 32-bit lua_Unsigned integer :
----------------------------------------------------------------
0.0000 0x00000000 bits 00000000000000000000000000000000
4294967295.0000 0xffffffff bits 11111111111111111111111111111111
Bitwise operators are used to change individual bits in an operand.
A single byte of computer memory-when viewed as 8 bits-can signify
the true/false status of 8 flags because each bit can be used as
a boolean variable that can hold one of two values: true or false.
A flag variable is typically used to indicate the status of something.
For instance, computer files can be marked as read-only.
So you might have a fReadOnly variable whose job would be to hold the read-only
status of a file.
This variable is called a flag variable because when fReadOnly has
a true value, it's equivalent to a football referee throwing a flag.
The variable says, "Whoa! Don't modify this file."
When you have more than one flag variable, it might be more efficient to use
a single variable to indicate the value of more than one flag.
The next example shows you how to do this.
Example: Using the bitwise AND,OR,NOT and XOR Operators
The first step to using bitwise operators to indicate more than one flag in
a single variable is to define the meaning of the bits that you'd like to use.
Figure shows an example of 8 bits that could be used to control the attributes
of text on a display.:
-- ---------------------------------------
-- | Value | Bit pos. | attribute |
-- ---------------------------------------
Italic = 128; -- | 128 | 7 | Italic |
Bold = 64; -- | 64 | 6 | Bold |
Blinking = 32; -- | 32 | 5 | Blinking |
Underline = 16; -- | 16 | 4 | Underline |
DblUnderline = 8; -- | 8 | 3 | Double_Underline |
-- | 4 | 2 | Future_use |
-- | 2 | 1 | Future_use |
-- | 1 | 0 | Future_use |
-- ---------------------------------------
txtattMask = ( Italic | Bol | Blinking | Underline | DblUnderline );
-- Set (add) attribute(s) to textAttr (bitwise OR ... | ):
textAttr = textAttr | Blinking;
textAttr = textAttr | ( Blinking | Italic | DblUnderine);
-- Check attribute(s) (bitwise AND ... & ):
if (textAttr & Blinking) then
print "is blinking"
end
if (textAttr & ( Blinking | Bold )) then
print "is blinking or bold or both"
end
-- Reset (remove) attribute(s) from textAttr (bitwise NOT ... ~ ):
textAttr = textAttr & ~( Blinking | Italic | DblUnderine);
-- Bold,Underline untouched
textAttr = textAttr & ~txtattMask;
-- remove all attributes
-- Switch attribute(s) (bitwise XOR ... ^^ )
textAttr = textAttr ^^ Blinking;
-- if was blinked, now is no-blinking
-- if was no-blinked, now is bliking
textAttr = textAttr ^^ (Blinking | Bold);
-- switch Blinking and Bold to opposite state
----------------------------------------------------------------
----------------------------------------------------------------
used values (in examples formated as %16.4f):
defaut op1 = math.random(-2147483647,2147483647)/3 -714036510.33333
defaut op2 = math.random(-2147483647,2147483647)/13 21007449.692308
used op1= arg[1]( nil ) ? default op1 : -714036510.33333
used op2= arg[2]( nil ) ? default op2 : 21007449.692308
op1 & op2 -- The bitwise AND operator compares two bits and generates
a result of 1 if both bits are 1;
otherwise, it returns 0.
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
&
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
21006402.0000 0x01408842 bits 00000001010000001000100001000010
op1 | op2 -- The OR operator compares two bits and generates
a result of 1 if the bits are complementary;
otherwise, it returns 0.
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
|
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
3580931834.0000 0xd570aefa bits 11010101011100001010111011111010
op1 ^^ op2 -- The EXCLUSIVE-OR operator compares two bits
and returns 1 if either of the bits are 1
and it gives 0 if both bits are 0 or 1.
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
^^
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
3559925432.0000 0xd43026b8 bits 11010100001100000010011010111000
~op1 -- The COMPLEMENT operator is used to invert
all of the bits of the operand.
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
~
================================================================
714036509.0000 0x2a8f551d bits 00101010100011110101010100011101
op1 >> op2 -- The SHIFT RIGHT operator moves the bits to the right,
discards the far right bit,
and assigns the leftmost bit a value of 0.
(op2 & 31) Each move to the right effectively divides op1 in half.
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
>>
26.0000 0x0000001a bits 00000000000000000000000000011010
================================================================
53.0000 0x00000035 bits 00000000000000000000000000110101
op1 << op2 -- The SHIFT LEFT operator moves the bits to the left,
discards the far left bit,
and assigns the rightmost bit a value of 0.
(op2 & 31) Each move to the left effectively multiplies op1 by 2.
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
<<
26.0000 0x0000001a bits 00000000000000000000000000011010
================================================================
2281701376.0000 0x88000000 bits 10001000000000000000000000000000
-------------------------------------------------------------------------------
L O G I C A L O P E R A T O R S (C-like, return 0 or 1)
http://www.cs.cf.ac.uk/Dave/PERL/node35.html
-------------------------------------------------------------------------------
The concept of logical operators is simple. They allow a program to make
a decision based on multiple conditions.
Each operand is considered a condition that can be evaluated to
a true or false value.
Then the value of the conditions is used to determine the overall value
of the op1 operator op2 or !op1 grouping.
&& ... logAND, || ... logOR, ! ... logNOT
Note !! Both operands associated with the logical operators cannot be nil. !!
in lualubit there is NO automatic conversion nill -> false
op1 && op2 -- Performs a logical AND of the two operands.
standart Lua something similar: op1 AND op2
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
&&
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
1.0000 0x00000001 bits 00000000000000000000000000000001
op1 || op2 -- Performs a logical OR of the two operands.
standart Lua something similar: op1 OR op2
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
||
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
1.0000 0x00000001 bits 00000000000000000000000000000001
! op1 -- Performs a logical NOT of the operand.
standart Lua something similar: NOT op1
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
||
================================================================
0.0000 0x00000000 bits 00000000000000000000000000000000
-------------------------------------------------------------------------------
L O G I C A L O P E R A T O R S (standart LUA)
http://www.lua.org/manual/5.2/manual.html#3.4.4
-------------------------------------------------------------------------------
The logical operators in Lua are and, or, and not.
Like the control structures, all logical operators consider
both false and nil as false and anything else as true.
Both and and or use short-cut evaluation;
that is, the second operand is evaluated only if necessary.
op1 and op2 -- The conjunction operator and returns its first argument
if this value is false or nil;
otherwise, and returns its second argument.
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
and
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
op1 or op2 -- The disjunction operator or returns its first argument
if this value is different from nil and false;
otherwise, or returns its second argument.
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
or
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
not op1 -- The negation operator not always returns false or true.
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
not
================================================================
0.0000 0x00000000 bits 00000000000000000000000000000000
-------------------------------------------------------------------------------
R E L A T I O N A L O P E R A T O R S (standart LUA)
http://www.lua.org/manual/5.2/manual.html#3.4.3
-------------------------------------------------------------------------------
The relational operators in Lua are
== ~= < > <= >=
These operators always result in false or true.
Equality (==) first compares the type of its operands.
If the types are different, then the result is false.
Otherwise, the values of the operands are compared.
Numbers and strings are compared in the usual way.
Objects (tables, userdata, threads, and functions) are compared by reference:
two objects are considered equal only if they are the same object.
Every time you create a new object (a table, userdata, thread, or function),
this new object is different from any previously existing object.
op1 == op2 -- The equality operator ==
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
==
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
0.0000 0x00000000 bits 00000000000000000000000000000000
op1 ~= op2 -- The unequality operator ==
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
~=
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
1.0000 0x00000001 bits 00000000000000000000000000000001
op1 > op2 -- The greater than operator >
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
>
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
0.0000 0x00000000 bits 00000000000000000000000000000000
op1 < op2 -- The less than operator <
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
<
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
1.0000 0x00000001 bits 00000000000000000000000000000001
op1 >= op2 -- The greater than or equal operator >=
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
>=
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
0.0000 0x00000000 bits 00000000000000000000000000000000
op1 <= op2 -- The less than or equal operator <=
----------------------------------------------------------------
-714036510.3333 0xd570aae2 bits 11010101011100001010101011100010
<=
21007449.6923 0x01408c5a bits 00000001010000001000110001011010
================================================================
1.0000 0x00000001 bits 00000000000000000000000000000001