bug-gawk
[Top][All Lists]
Advanced

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

Re: Support for negative arguments for bitwise functions


From: arnold
Subject: Re: Support for negative arguments for bitwise functions
Date: Mon, 20 Mar 2023 08:06:34 -0600
User-agent: Heirloom mailx 12.5 7/5/10

Hi.

Thank you for the note. The bytebeat stuff is rather cool.

(To be honest, I wish I'd never added the bitwise functions to gawk, but
that was a youuthful mistake made long ago now.)

Numbers in gawk are by default double-precision floating point values
(C doubles).  For the bitwise functions, gawk converts them to
uintmax_t values - unsigned.  This is the reason negative values
are not allowed, as converting them to unsigned makes no sense.

I have also found that whenever (in C) I do bitwise stuff on signed
values, I end up with sometimes weird results due to sign extension
and the promotion rules.  Thus the use of unsigned in gawk, where
bitwise operations are clearly defined and portable.

In short, I think allowing negative values for the bitwise functions
is likely to cause more problems than it solves.  Sorry.

Arnold

Volodymyr Gubarkov <xonixx@gmail.com> wrote:

> Hi,
>
> Just for fun I was playing with some Bytebeat (
> http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html)
> tunes and tried to convert to Gawk.
>
> While the result was successful (https://www.youtube.com/watch?v=qOenoyBO7XY)
> I found it uncomfortable that bitwise functions do not support negative
> arguments, so the code in C as simple as
> https://github.com/xonixx/bytebeat-gawk/blob/main/a3.c in Gawk becomes
> https://github.com/xonixx/bytebeat-gawk/blob/main/a3final.awk .
>
> I see the reasoning in
> https://www.gnu.org/software/gawk/manual/html_node/Bitwise-Functions.html#index-sidebar-22
> but somehow other languages, even the dynamic ones manage to do it
> consistently with C:
>
> C:
> $ echo
> 'main(){printf("%d\n",5^2);printf("%d\n",-5^2);printf("%d\n",5^-2);printf("%d\n",-5^-2);}'
> | tcc -w -run -
> 7
> -7
> -5
> 5
>
> Javascript (node + browsers):
> $ node -e
> 'console.log(5^2);console.log(-5^2);console.log(5^-2);console.log(-5^-2);'
> 7
> -7
> -5
> 5
>
> Python:
> $ python3 -c 'print(5^2);print(-5^2);print(5^-2);print(-5^-2)'
> 7
> -7
> -5
> 5
>
> Gawk:
> $ gawk 'BEGIN { xor(-5,2) }'
> gawk: cmd. line:1: fatal: xor: argument 2 negative value -5 is not allowed
>
> Does it make sense to consider the approach of nodejs at implementing the
> bitwise operators for negative numbers?
>
> >JavaScript stores numbers as 64 bits floating point numbers, but all
> bitwise operations are performed on 32 bits binary numbers.
> >Before a bitwise operation is performed, JavaScript converts numbers to 32
> bits signed integers.
> >After the bitwise operation is performed, the result is converted back to
> 64 bits JavaScript numbers.
>
> Best regards,
> Volodymyr



reply via email to

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