[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Support for negative arguments for bitwise functions
From: |
Volodymyr Gubarkov |
Subject: |
Support for negative arguments for bitwise functions |
Date: |
Sun, 19 Mar 2023 15:35:15 +0200 |
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
- Support for negative arguments for bitwise functions,
Volodymyr Gubarkov <=