
URGENT: Need help with AND binary operation in awk
Quote:
> Could anyone help me with the following:
> I have a decimal number nnnnnnnn in variable Var1
> I need to convert this to it's binary equivalent store it in a variable
> Var2.
> Take Var2 and AND (binary AND) with another binary number 0000000 and
> store the result in Var3.
> An equivalently good one would be to replace the last x number of digits
> in the binary variable Var2 with 0s.
> I need to do this within awk.
> Thank you
gawk 3.1 has binary bit operators.
Before that, I did something like this:
function and(a, b , res, p2, abit, bbit)
{
res = 0; # the result
p2 = 1; # power of 2 - the bit
while ( (a > 0) || (b > 0) )
{
abit = a % 2;
bbit = b % 2;
a = (a-abit)/2
b = (b-bbit)/2
if ( abit && bbit )
{
res = res + p2
}
p2 += p2
}
return res
Quote:
}
Grungy and slow, but (unless typos here) it worked.
Martin Cohen