2

Very strangely, I found that in awk, the big integer looks like has only 53 bits.Here is my example:

function bits2str(bits,data, mask)
{
    if (bits == 0)
        return "0"

    mask = 1
    for (; bits != 0; bits = rshift(bits, 1))
        data = (and(bits, mask) ? "1" : "0") data

    while ((length(data) % 8) != 0)
        data = "0" data

    return data
}

BEGIN{
    print 32,"\tlshift 48:\t", lshift(32,48), "\t", bits2str(lshift(32,48))
    print 429,"\tlshift 48:\t", lshift(429,48), "\t", bits2str(lshift(429,48))
}

and the output is:

32  lshift 48:   0   0
429     lshift 48:   3659174697238528    00001101000000000000000000000000000000000000000000000000

but in c++, its output is:

32 lshift 48: 9007199254740992
429 lshift 48: 120752765008871424

After comparing the two output, I found that the awk's only have 53 bits, and then I researched the source code of gawk(start from line 3021 in the file named builtin.c, gawk 4.1.1, http://ftp.gnu.org/gnu/gawk/), but I found no special operation on int. So, what causes this? Why it is like this?

2 Answers 2

2

In AWK, all numbers are stored in floating point.

From Bitwise function:

For all of these functions, first the double precision floating-point value is converted to the widest C unsigned integer type, then the bitwise operation is performed. If the result cannot be represented exactly as a C double, leading nonzero bits are removed one by one until it can be represented exactly. The result is then converted back into a C double.

Assuming IEEE-754 is used, doubles can only represent integers up to 253.

2

if you use gawk, you need add the -M option for big number.

kent$ awk 'BEGIN{print lshift(32,48)}'
0

kent$ awk -M 'BEGIN{print lshift(32,48)}'
9007199254740992
3
  • No, it doesn't work. My gawk/awk has no this option. My awk's version is GNU Awk 3.1.5, how about you?
    – xingwudao
    Jun 24, 2014 at 9:37
  • 1
    In your question, you mentioned you checked gawk 4.1.1's codes. so I tested with 4.1.1. If you are working on 3.1.5, why you checked 4.1.1's codes? @xingwudao
    – Kent
    Jun 24, 2014 at 10:01
  • Aha, sorry, it's such a stupid trying for me! I will install 4.1.1 on my machine.
    – xingwudao
    Jun 24, 2014 at 10:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.