Node:Integer Logic and Bit Fiddling, Next:, Previous:Integer Comparisons, Up:Integer Functions



Logical and Bit Manipulation Functions

These functions behave as if twos complement arithmetic were used (although sign-magnitude is the actual implementation). The least significant bit is number 0.

void mpz_and (mpz_t rop, mpz_t op1, mpz_t op2) Function
Set rop to op1 logical-and op2.

void mpz_ior (mpz_t rop, mpz_t op1, mpz_t op2) Function
Set rop to op1 inclusive-or op2.

void mpz_xor (mpz_t rop, mpz_t op1, mpz_t op2) Function
Set rop to op1 exclusive-or op2.

void mpz_com (mpz_t rop, mpz_t op) Function
Set rop to the one's complement of op.

unsigned long int mpz_popcount (mpz_t op) Function
If op>=0, return the population count of op, which is the number of 1 bits in the binary representation. If op<0, the number of 1s is infinite, and the return value is MAX_ULONG, the largest possible unsigned long.

unsigned long int mpz_hamdist (mpz_t op1, mpz_t op2) Function
If op1 and op2 are both >=0 or both <0, return the hamming distance between the two operands, which is the number of bit positions where op1 and op2 have different bit values. If one operand is >=0 and the other <0 then the number of bits different is infinite, and the return value is MAX_ULONG, the largest possible unsigned long.

unsigned long int mpz_scan0 (mpz_t op, unsigned long int starting_bit) Function
unsigned long int mpz_scan1 (mpz_t op, unsigned long int starting_bit) Function
Scan op, starting from bit starting_bit, towards more significant bits, until the first 0 or 1 bit (respectively) is found. Return the index of the found bit.

If the bit at starting_bit is already what's sought, then starting_bit is returned.

If there's no bit found, then MAX_ULONG is returned. This will happen in mpz_scan0 past the end of a positive number, or mpz_scan1 past the end of a negative.

void mpz_setbit (mpz_t rop, unsigned long int bit_index) Function
Set bit bit_index in rop.

void mpz_clrbit (mpz_t rop, unsigned long int bit_index) Function
Clear bit bit_index in rop.

int mpz_tstbit (mpz_t op, unsigned long int bit_index) Function
Test bit bit_index in op and return 0 or 1 accordingly.