| [ << ] | [ < ] | [ 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
~xin 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 & yin 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 | yin 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 ^ yin 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 & yin C. cl_I logandc2 (const cl_I& x, const cl_I& y)-
Logical and, complementing the second argument, like
x & ~yin C. cl_I logorc1 (const cl_I& x, const cl_I& y)-
Logical or, complementing the first argument, like
~x | yin C. cl_I logorc2 (const cl_I& x, const cl_I& y)-
Logical or, complementing the second argument, like
x | ~yin 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
xandy, i.e. iflogand(x,y) != 0. bool logbitp (const cl_I& n, const cl_I& x)-
Returns true if the
nth bit (from the right) ofxis 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
ndescribed by the bit intervalband returns them as a nonnegative integer withb.sizebits. bool ldb_test (const cl_I& n, const cl_byte& b)-
Returns true if some bit described by the bit interval
bis 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 intervalbreplaced bynewbyte. Only the lowestb.sizebits ofnewbyteare 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
bcopied 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
bcome fromnewbyteand 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
xis 1. Equivalent tomod(x,2) != 0. bool evenp (const cl_I& x)-
Returns true if the least significant bit of
xis 0. Equivalent tomod(x,2) == 0. cl_I operator << (const cl_I& x, const cl_I& n)-
Shifts
xbynbits to the left.nshould be >=0. Equivalent tox * expt(2,n). cl_I operator >> (const cl_I& x, const cl_I& n)-
Shifts
xbynbits to the right.nshould 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
xbyybits to the left (ify>=0) or by-ybits 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
xin 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)-
xmust be non-zero. This function returns the number of 0 bits at the right ofxin two’s complement notation. This is the largest n >= 0 such that 2^n dividesx. uintC power2p (const cl_I& x)-
xmust be > 0. This function checks whetherxis 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.
