Professional Documents
Culture Documents
Binary Logic in The R Epository: A) Takes N As Parameter
Binary Logic in The R Epository: A) Takes N As Parameter
Binary Logic in The R Epository: A) Takes N As Parameter
wtr/2001-11-20
Definitions
S_Vector: binary Value of Statevariable S (currently defined for N=[1..30])
N=Bitnumber: Position in S; count starts at 1.
Bit_Value:=2**(N-1): Value of Bit N is set to 1 (_true_), N=3 --> Value=4
Bit_Vector: some binary Value, where potentially many Bits have a value of 1;
the value can be extended by directly arithmetically adding Bit_Values;
the value can be decreased by directly arithmetically subtracting Bit_Values.
However this quite dangerous, as the same Bits must not
be used in more than 1 operation of the same type (add, subtract) in a row!
a) takes N as Parameter
Set_Bit_1(N,S_Vector): OR(S_Vector,Bit_Value)
*Set Bit N in S to 1*
The function has to compute Bit_Value=2**(N-1), then OR.
Set_Bit_0(N,S_Vector): AND(S_Vector,Bit_Value)
*Set Bit N in S to 0*
The function has to compute
COMPLEMENT(2**(N-1)),
then AND.
The COMPLEMENT (or NOT) sets all Bits minus number N to 1.
The result of the following AND sets Bit N in S to 0.
The same result would be achieved by arithmetically subtracting the Bit_Value from S, but that is very
dangerous (see above), especially since the function cannot know, if the same operation has been
requested immediately before.
Get_Bit(check_N,S_Vector): AND(S_Vector,Bit_Value)
*Get value of Bit N in S*
The function has to compute 2**(N-1), then AND.
The result is <> 0, if the Bit number check_N is 1.
Set_Bits_1(Bit_Vector,S_Vector): OR(S_Vector,Bit_Vector)
*Set the Bits of S with value 1 in the Bit_Vector to 1.*
Get_Bits(check_Bit_Vector,S_Vector): AND(S_Vector,Bit_Vector)
*Get Bit_Vector of all Bits in the check set.*
The resulting value is only 0, iff no Bit is 1.
If all Bits have to be set exactly as the Bit_Vector, then of course a direct assignment should
be done:
S=BV.