### Logic.BC - Do bitwise functions with GNU bc # Perform a bitwise logical AND of x and y define and(x,y) { auto z, t, xx, yy, os; os=scale;scale=0 z=0;t=1;for(;x||y;){ xx=x/2;yy=y/2 z+=t*((x-2*xx)&&(y-2*yy)) t*=2;x=xx;y=yy } scale=os;return (z) } # Perform a bitwise logical OR of x and y define or(x,y) { auto z, t, xx, yy, os; os=scale;scale=0 z=0;t=1;for(;x||y;){ xx=x/2;yy=y/2 z+=t*((x-2*xx)||(y-2*yy)) t*=2;x=xx;y=yy } scale=os;return (z) } # Perform a bitwise logical EXCLUSIVE-OR of x and y define xor(x,y) { auto z, t, xx, yy, os; os=scale;scale=0 z=0;t=1;for(;x||y;){ xx=x/2;yy=y/2 z+=t*((x-2*xx)!=(y-2*yy)) t*=2;x=xx;y=yy } scale=os;return (z) } # Perform a bitwise logical NOT of x define not(x,w) { # w is bit width # w is implicitly expanded to the bit-width of x if # it is not wide enough auto z, t, xx, os; os=scale;scale=0 z=0;t=1;for(;x||(w>0);w--){ xx=x/2;z+=t*(1-x+2*xx) t*=2;x=xx } scale=os;return (z) } # Perform a bitwise logical LEFT-SHIFT of x by n places define shl(x,n,w) { # w is bit width auto os;os=scale;scale=0 x=(x*2^n)%(2^w) scale=os;return(x) } # Perform a bitwise logical RIGHT-SHIFT of x by n places define shr(x,n,w) { # w is bit width auto os;os=scale;scale=0 x=(x/2^n)%(2^w) scale=os;return(x) } # Reverse bits in x define bitrev(x,w) { # w is bit width, w = 0 -> use number if bits in x auto os,z;os=scale;scale=0 if(w){x%=(2^w)}else{for(;2^w