Question

How can one specify a number as binary in gawk?

According to the manual, gawk interprets all numbers as decimal unless they are preceded by a 0 (octal) or by a 0x (hexadecimal). Unlike in certain other languages, 0b does not do the trick.

For instance, the following lines do not give the desired output (010000 or 10000) because the values are interpreted as octal/decimal or decimal/decimal, respectively:

gawk '{print and(010000,110000)}'
0

gawk '{print and(10000,110000)}'
9488

I suspect that gawk may not support base-2 and that a user-defined function will be required to generate binary representations.

Was it helpful?

Solution

You're right, there's no internal support for binary conversion in gawk. And incredibly, there isn't even any in printf(). So you're stuck with functions.

Remember that awk is weakly typed. Which is why functions have insane behaviours like recognizing that "0x" at the beginning of a string means it's a hexadecimal number. In a language like this, better to control your own types.

Here's a couple of functions I've had sitting around for years...

#!/usr/local/bin/gawk -f

function bin2dec(n) {
  result = 0;
  if (n~/[^01]/) {
    return n;
  }
  for (i=length(n); i; i--) {
    result += 2^(length(n)-i) * substr(n,i,1);
  }
  return result;
}

function dec2bin(n) {
  result = "";
  while (n) {
    if (n%2) {
      result = "1" result;
    } else {
      result = "0" result;
    }
    n = int(n/2);
  }
  return result;
}

{
  print dec2bin( and(bin2dec($1),bin2dec($2)) );
}

And the result:

$ echo "1101 1011" | ./doit.awk
1001
$ echo "11110 10011" | ./doit.awk
10010
$ 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top