bug-gawk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-gawk] bitwise functions with negative values


From: Andrew J. Schorr
Subject: [bug-gawk] bitwise functions with negative values
Date: Tue, 4 Oct 2016 20:16:58 -0400
User-agent: Mutt/1.5.23 (2014-03-12)

In comp.lang.awk, a few issues have been raised recently regarding the behavior
of bitwise functions with negative values.

https://groups.google.com/forum/#!topic/comp.lang.awk/TXKbQGT36qY

In the docs, there's a str2bits function that goes into an infinite loop
when run with -M (--bignum). That's because with -M, we have this
behavior:

bash-4.3$ gawk -M 'BEGIN {print rshift(-1, 1)}'
-1

That's because the implementation in do_mpfr_rshift uses mpz_fdiv_q_2exp.
If we were to change it to mpz_tdiv_q_2exp, the result would be zero.
That would eliminate the infinite loop, but I'm not sure what behavior
makes most sense.

Also, the "compl" function gives wildly different results when used with -M.
For example:

bash-4.3$ gawk 'BEGIN {print compl(5)}'
18014398509481978
bash-4.3$ gawk -M 'BEGIN {print compl(5)}'
-6

Also, I think it's pretty weird that bits2str in the doc uses 
zero-padding on the left. That gives strange results for negative values.

It's not clear how to address these various issues. One idea is to stop
supporting negative values with these bitwise functions. And I'm not sure
whether compl makes any sense at all. Thoughts?

Regards,
Andy



reply via email to

[Prev in Thread] Current Thread [Next in Thread]