/* -*- gp-script -*- */ \\% Parity, bitcount and Gray code. \\ Author: Joerg Arndt \\ License: GPL version 3 or later \\ online at http://www.jjj.de/pari/ \\ version: 2014-October-16 (18:30) bitcount(x)= { /* Return Hamming weight of x */ my(p=0); while ( x, p+=bitand(x, 1); x>>=1; ); return( p ); } /* ----- */ \\parity(x)= return( bitand(bitcount(x), 1) ); parity(x)= { my(s=1); while ( (x>>s), x=bitxor(x, x>>s); s+=s; ); return( bitand(x,1) ); } /* ----- */ gray(k)= return ( bitxor(k,k>>1) ); igray(x)= { my( s, xs ); s = 1; while ( 1, xs = x >> s; if ( 0==xs, break() ); x = bitxor(x, xs); s <<= 1; ); return ( x ); } /* ----- */ is_bitsubset(u, e)= \\ Return whether the bits of u are a subset of the bits of e. { return( bitand(u,e)==u ); } \\ ==== end of file ====