lua-users home
lua-l archive

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


Forgive me if this has been previously noted:
The 5.3 manual says
>  All bitwise operations convert its operands to integers (see §3.4.3), operate on all bits of those integers, and result in an integer.
> Both right and left shifts fill the vacant bits with zeros.

In other words an "unsigned" shift or logical shift. No sign extension.

Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
> string.format("0x%016x",(1<<63)>>8)
0x0080000000000000

Bash
# printf '0x%016x\n' $(( (1<<63)>>8 ))
0xff80000000000000

PHP
printf '0x%016x\n' $(( (1<<63)>>8 ))
0xff80000000000000

Perl
# perl -e 'printf "0x%016x\n",(1<<63)>>8'
0x0080000000000000

SQLite 3.29.0
sqlite> select printf('0x%016x',(1<<63)>>8);
0xff80000000000000

*I wish this was highlighted in the manual.*

What was the reasoning in choosing logical shift for >> rather than arithmetic?
It seems strange given that >> only operates on lua_integer and
lua_integer is by default a signed entity.
also note:
> string.format("%d , %d",(1<<63),(1<<63)//256)
-9223372036854775808 , -36028797018963968
> string.format("%d , %d",(1<<63),(1<<63)//(2^8))
-9223372036854775808 , -36028797018963968
> string.format("%d , %d",(1<<63),(1<<63)>>8)
-9223372036854775808 , 36028797018963968