为什么Perl认为1 << - 1是9223372036854775808?

Zai*_*aid -1 perl bit-shift

如果我理解perldoc perlop正确,这个操作应该是未定义的:

溢出整数范围的结果是未定义的,因为它在C中也是未定义的.换句话说,使用32位整数1 << 32是未定义的.通过负位数移位也是不确定的.


我注意到我的设置中有两件事我无法解释:

  1. 1 << -1返回9223372036854775808without bigintintegerpragma(NaN在它们处于活动状态时返回).

    > perl -le "print 1<<-1"
      9223372036854775808
    
    Run Code Online (Sandbox Code Playgroud)
  2. 1 << -1在测试时返回true defined,无论bigintintegerpragma是否生效

    > 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
    
    Run Code Online (Sandbox Code Playgroud)

我的设置细节:

> 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
    .
Run Code Online (Sandbox Code Playgroud)

Cam*_*ron 12

当他们说未定义时,他们指的是未定义的行为,而不是价值undef.

如果操作的结果是未定义的行为,则意味着任何事情都可能发生 - 您可能会得到令人惊讶的结果,不同平台上的结果不同,每次执行相同的操作时会产生不同的结果,或者3英尺高的蜗牛可能会从你的计算机出来并开始说斯瓦希里语(虽然最后一个在实践中相当罕见).

换句话说,因为它被明确记录为未定义的行为,所以perl所做的任何事情在技​​术上都是正确的 - 这意味着你不能依赖它来做任何有用的事情.