Binary Logic in The R Epository: A) Takes N As Parameter

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

Binary Logic in the Repository

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!

Some Get/Set functions


One Parameter is always S, but the other is either a Bit_Number N or a Bit_Value.

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.

b) takes a Bit_Vector/Bit_Value as Parameter (the caller did the 2**(N-1)


computations)

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.

This is useful, when some combination of Bits are used to express


a compound fact, like "if a mail has been delivered, is blocked or
has been changed it may be archived". In this case, the archiver may check
all 3 Bits in 1 operation and if the value is <> 0, it may proceed.

If all Bits have to be set exactly as the Bit_Vector, then of course a direct assignment should
be done:
S=BV.

You might also like