[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
4.9.1 Logical functions
Integers, when viewed as in two’s complement notation, can be thought as infinite bit strings where the bits’ values eventually are constant. For example,
17 = ......00010001 -6 = ......11111010
The logical operations view integers as such bit strings and operate on each of the bit positions in parallel.
cl_I lognot (const cl_I& x)
cl_I operator ~ (const cl_I& x)
-
Logical not, like
~x
in C. This is the same as-1-x
. cl_I logand (const cl_I& x, const cl_I& y)
cl_I operator & (const cl_I& x, const cl_I& y)
-
Logical and, like
x & y
in C. cl_I logior (const cl_I& x, const cl_I& y)
cl_I operator | (const cl_I& x, const cl_I& y)
-
Logical (inclusive) or, like
x | y
in C. cl_I logxor (const cl_I& x, const cl_I& y)
cl_I operator ^ (const cl_I& x, const cl_I& y)
-
Exclusive or, like
x ^ y
in C. cl_I logeqv (const cl_I& x, const cl_I& y)
-
Bitwise equivalence, like
~(x ^ y)
in C. cl_I lognand (const cl_I& x, const cl_I& y)
-
Bitwise not and, like
~(x & y)
in C. cl_I lognor (const cl_I& x, const cl_I& y)
-
Bitwise not or, like
~(x | y)
in C. cl_I logandc1 (const cl_I& x, const cl_I& y)
-
Logical and, complementing the first argument, like
~x & y
in C. cl_I logandc2 (const cl_I& x, const cl_I& y)
-
Logical and, complementing the second argument, like
x & ~y
in C. cl_I logorc1 (const cl_I& x, const cl_I& y)
-
Logical or, complementing the first argument, like
~x | y
in C. cl_I logorc2 (const cl_I& x, const cl_I& y)
-
Logical or, complementing the second argument, like
x | ~y
in C.
These operations are all available though the function
where op
must have one of the 16 values (each one stands for a function
which combines two bits into one bit): boole_clr
, boole_set
,
boole_1
, boole_2
, boole_c1
, boole_c2
,
boole_and
, boole_ior
, boole_xor
, boole_eqv
,
boole_nand
, boole_nor
, boole_andc1
, boole_andc2
,
boole_orc1
, boole_orc2
.
Other functions that view integers as bit strings:
bool logtest (const cl_I& x, const cl_I& y)
-
Returns true if some bit is set in both
x
andy
, i.e. iflogand(x,y) != 0
. bool logbitp (const cl_I& n, const cl_I& x)
-
Returns true if the
n
th bit (from the right) ofx
is set. Bit 0 is the least significant bit. uintC logcount (const cl_I& x)
-
Returns the number of one bits in
x
, ifx
>= 0, or the number of zero bits inx
, ifx
< 0.
The following functions operate on intervals of bits in integers. The type
struct cl_byte { uintC size; uintC position; };
represents the bit interval containing the bits
position
…position+size-1
of an integer.
The constructor cl_byte(size,position)
constructs a cl_byte
.
cl_I ldb (const cl_I& n, const cl_byte& b)
-
extracts the bits of
n
described by the bit intervalb
and returns them as a nonnegative integer withb.size
bits. bool ldb_test (const cl_I& n, const cl_byte& b)
-
Returns true if some bit described by the bit interval
b
is set inn
. cl_I dpb (const cl_I& newbyte, const cl_I& n, const cl_byte& b)
-
Returns
n
, with the bits described by the bit intervalb
replaced bynewbyte
. Only the lowestb.size
bits ofnewbyte
are relevant.
The functions ldb
and dpb
implicitly shift. The following
functions are their counterparts without shifting:
cl_I mask_field (const cl_I& n, const cl_byte& b)
-
returns an integer with the bits described by the bit interval
b
copied from the corresponding bits inn
, the other bits zero. cl_I deposit_field (const cl_I& newbyte, const cl_I& n, const cl_byte& b)
-
returns an integer where the bits described by the bit interval
b
come fromnewbyte
and the other bits come fromn
.
The following relations hold:
-
ldb (n, b) = mask_field(n, b) >> b.position
, -
dpb (newbyte, n, b) = deposit_field (newbyte << b.position, n, b)
, -
deposit_field(newbyte,n,b) = n ^ mask_field(n,b) ^ mask_field(new_byte,b)
.
The following operations on integers as bit strings are efficient shortcuts for common arithmetic operations:
bool oddp (const cl_I& x)
-
Returns true if the least significant bit of
x
is 1. Equivalent tomod(x,2) != 0
. bool evenp (const cl_I& x)
-
Returns true if the least significant bit of
x
is 0. Equivalent tomod(x,2) == 0
. cl_I operator << (const cl_I& x, const cl_I& n)
-
Shifts
x
byn
bits to the left.n
should be >=0. Equivalent tox * expt(2,n)
. cl_I operator >> (const cl_I& x, const cl_I& n)
-
Shifts
x
byn
bits to the right.n
should be >=0. Bits shifted out to the right are thrown away. Equivalent tofloor(x / expt(2,n))
. cl_I ash (const cl_I& x, const cl_I& y)
-
Shifts
x
byy
bits to the left (ify
>=0) or by-y
bits to the right (ify
<=0). In other words, this returnsfloor(x * expt(2,y))
. uintC integer_length (const cl_I& x)
-
Returns the number of bits (excluding the sign bit) needed to represent
x
in two’s complement notation. This is the smallest n >= 0 such that -2^n <= x < 2^n. If x > 0, this is the unique n > 0 such that 2^(n-1) <= x < 2^n. uintC ord2 (const cl_I& x)
-
x
must be non-zero. This function returns the number of 0 bits at the right ofx
in two’s complement notation. This is the largest n >= 0 such that 2^n dividesx
. uintC power2p (const cl_I& x)
-
x
must be > 0. This function checks whetherx
is a power of 2. Ifx
= 2^(n-1), it returns n. Else it returns 0. (See also the functionlogp
.)
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on August 27, 2013 using texi2html 5.0.