为什么Perl认为1<<-1是9223372036854775808吗?
如果我理解正确,此操作应该是未定义的:为什么Perl认为1<<-1是9223372036854775808吗?,perl,bit-shift,Perl,Bit Shift,如果我理解正确,此操作应该是未定义的: 溢出整数范围的结果是未定义的,因为它在C中也是未定义的。换句话说,使用32位整数,1当他们说未定义时,他们指的是,而不是值undef 如果一个操作的结果是未定义的行为,这意味着任何事情都可能发生——你可能会得到一个令人惊讶的结果,在不同的平台上会得到不同的结果,每次你做同样的事情时会得到不同的结果,或者3英尺高的蜗牛可能从你的电脑里出来,开始说斯瓦希里语(尽管最后一个在实践中并不常见) 换句话说,因为它被明确地记录为未定义的行为,perl所做的任何事情在技
溢出整数范围的结果是未定义的,因为它在C中也是未定义的。换句话说,使用32位整数,
1当他们说未定义时,他们指的是,而不是值undef
如果一个操作的结果是未定义的行为,这意味着任何事情都可能发生——你可能会得到一个令人惊讶的结果,在不同的平台上会得到不同的结果,每次你做同样的事情时会得到不同的结果,或者3英尺高的蜗牛可能从你的电脑里出来,开始说斯瓦希里语(尽管最后一个在实践中并不常见)
换句话说,因为它被明确地记录为未定义的行为,perl所做的任何事情在技术上都是正确的——这意味着你不能依靠它来做任何有用的事情。数字9223372036854775808等于2^63。给定一个初始化为1的64位无符号整数,将其向右旋转一位就会得到这个结果。这是pr可能发生了什么:它的意思是“未定义的”,如“未定义的行为”。您的C编译器可能会提取移位输入的低6位,因为它需要6位输入(0到63之间的数字)。1
> perl -le "print 1<<-1"
9223372036854775808
> perl -le "print 'yes' if defined(1<<-1)"
yes
> perl -le "use integer; print 'yes' if defined(1<<-1)"
yes
> perl -le "use bigint; print 'yes' if defined(1<<-1)"
yes
> perl -V
Set up gcc environment - gcc.exe (rubenvb-4.5.4) 4.5.4
Summary of my perl5 (revision 5 version 16 subversion 3) configuration:
Platform:
osname=MSWin32, osvers=5.2, archname=MSWin32-x64-multi-thread
uname=''
config_args='undef'
hint=recommended, useposix=true, d_sigaction=undef
useithreads=define, usemultiplicity=define
useperlio=define, d_sfio=undef, uselargefiles=define, usesocks=undef
use64bitint=define, use64bitall=undef, uselongdouble=undef
usemymalloc=n, bincompat5005=undef
Compiler:
cc='C:\Perl64\site\bin\gcc.exe', ccflags ='-DNDEBUG -DWIN32 -D_CONSOLE -DNO_STRICT -DWIN64 -DCONSERVATIVE -DPERL_TEXTMODE_SCRIPTS -DUSE_SITECUSTOMIZE -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DHASATTRIBUTE -fno-strict-aliasing -mms-bitfields',
optimize='-O2',
cppflags='-DWIN32'
ccversion='', gccversion='gcc.exe (rubenvb-4.5.4) 4.5.4', gccosandvers=''
intsize=4, longsize=4, ptrsize=8, doublesize=8, byteorder=12345678
d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=8
ivtype='__int64', ivsize=8, nvtype='double', nvsize=8, Off_t='__int64', lseeksize=8
alignbytes=8, prototype=define
Linker and Libraries:
ld='C:\Perl64\site\bin\g++.exe', ldflags ='-L"C:\Perl64\lib\CORE"'
libpth=\lib
libs=-lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 -lmsvcrt
perllibs=-lkernel32 -luser32 -lgdi32 -lwinspool -lcomdlg32 -ladvapi32 -lshell32 -lole32 -loleaut32 -lnetapi32 -luuid -lws2_32 -lmpr -lwinmm -lversion -lodbc32 -lodbccp32 -lcomctl32 -lmsvcrt
libc=msvcrt.lib, so=dll, useshrplib=true, libperl=perl516.lib
gnulibc_version=''
Dynamic Linking:
dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' '
cccdlflags=' ', lddlflags='-mdll -L"C:\Perl64\lib\CORE"'
Characteristics of this binary (from libperl):
Compile-time options: HAS_TIMES HAVE_INTERP_INTERN MULTIPLICITY
PERLIO_LAYERS PERL_DONT_CREATE_GVSV
PERL_IMPLICIT_CONTEXT PERL_IMPLICIT_SYS
PERL_MALLOC_WRAP PERL_PRESERVE_IVUV PL_OP_SLAB_ALLOC
USE_64_BIT_INT USE_ITHREADS USE_LARGE_FILES
USE_LOCALE USE_LOCALE_COLLATE USE_LOCALE_CTYPE
USE_LOCALE_NUMERIC USE_PERLIO USE_PERL_ATOF
USE_SITECUSTOMIZE
Locally applied patches:
ActivePerl Build 1603 [296746]
Built under MSWin32
Compiled at Mar 13 2013 13:31:10
@INC:
C:/Perl64/site/lib
C:/Perl64/lib
.