看到标题,可能会心生疑惑: 这么基础且重要的操作,不同语言不应该是一致的吗?
并不一定,比如对于右移运算和加法运算,Go就与其他多数语言表现得不一致:
Go:
1234567 package mainimport "fmt"func main() { fmt.Println(1+2>>1) // 2}
Java:
12345678 import java.io.*;class test { public static void main (String[] args) throws java.lang.Exception { System.out.println(1+2>>1); // 1 }}
C/C++:
12345678 #include <stdio.h>int main(void) { int a = 1+2>>1; printf("%d\n",a); // 1 return 0;}
nodejs:
1 console.log(1+2>>1); // 1
python:
1234567 #!/usr/bin/python# -*- coding: utf-8 -*-import sysif True : aaa=1+2>>1 print(aaa) // 1 print(sys.version) //3.8.6 (default, Sep 24 2020, 21:45:12) [GCC 8.3.0]
php:
123456 <?php$aa=1+2>>1;echo 'hello '.'1+2>>1运算结果为:'.$aa."\n"; // hello 1+2>>1运算结果为:1echo date('Y-m-d H:i:s',time())."\n"; // 20xx-08-28 14:33:23echo "PHP版本:".phpversion(); // PHP版本:7.4.10?>