Java中>>、>>>、<<

定义:

右移动>>:带符号右移。正数右移高位补0,负数右移高位补1。

左移动<<:带符号左移。正数左移低位补0,负数左移低位补1。

无符号右移>>>:无论是正数还是负数,高位通通补0。

 

栗子:下面以32位表示

2的二进制:0000 0000 0000 0000 0000 0000 0000 0010

-2的二进制:1111 1111 1111 1111 1111 1111 1111 1110

获取2的负数二进制表示:2的二进制补码加1

  1111 1111 1111 1111 1111 1111 1111 1101

  加1

  1111 1111 1111 1111 1111 1111 1111 1110

 

<1、2>>>1 

        1)去掉红色数字:0000 0000 0000 0000 0000 0000 0000 0010 

      2)高位补0       0000 0000 0000 0000 0000 0000 0000 0001

<2、2>>1

        1 ) 去掉红色数字:0000 0000 0000 0000 0000 0000 0000 0010 

      2)高位补0       0000 0000 0000 0000 0000 0000 0000 0001

<3、-2>>1

      1) 去掉红色数字 :1111 1111 1111 1111 1111 1111 1111 1110

      2)高位补1       :1111 1111 1111 1111 1111 1111 1111 1111

<4、2<<1

      1 ) 去掉红色数字 :0000 0000 0000 0000 0000 0000 0000 0010

      2 )  低位补0       :0000 0000 0000 0000 0000 0000 0000 0100

<5、-2<<1

      1 ) 去掉红色数字 : 1111 1111 1111 1111 1111 1111 1111 1110

      2 ) 低位补1        : 1111 1111 1111 1111 1111 1111 1111 1101

<6、-2>>>1

      1 ) 去掉红色数字 :1111 1111 1111 1111 1111 1111 1111 1110

      2 ) 高位补0        :0111 1111 1111 1111 1111 1111 1111 1111

总结:

2>>>1 相当于2乘以2^1 同理2>>>2^n相当于乘以2^n 无符号位操作都补0

2>>1   相当于2乘以2^1 同理2>>>2^n相当于乘以2^n 正数高位补0,负数高位不1

2<<1   相当于2乘以2^1 同理2>>>2^n相当于乘以2^n 正数左移低位补0,负数左移低位补1。

 

 

 

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.rain4cloud</groupId> <version>1.0-SNAPSHOT</version> <artifactId>grpc-api</artifactId> <properties> <maven.compiler.source>17</maven.compiler.source> <maven.compiler.target>17</maven.compiler.target> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <!-- 这里不需要手动设置 os.detected.classifier,插件会自动检测 --> <!--<os.detected.classifier>windows-x86_64</os.detected.classifier>--> <!--mac M暂未兼容,使用x86--> <os.detected.classifier>osx-x86_64</os.detected.classifier> <protobuf-maven-plugin.version>0.6.1</protobuf-maven-plugin.version> <protoc.version>3.25.3</protoc.version> <protobuf.java.version>3.25.3</protobuf.java.version> <grpc-java.version>1.70.0</grpc-java.version> </properties> <dependencies> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>1.70.0</version> <exclusions> <exclusion> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> <version>1.70.0</version> <exclusions> <exclusion> <groupId>javax.annotation</groupId> <artifactId>javax.annotation-api</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-api</artifactId> <version>1.70.0</version> </dependency> <!-- javax.annotation API --> <!-- <dependency>--> <!-- <groupId>javax.annotation</groupId>--> <!-- <artifactId>javax.annotation-api</artifactId>--> <!-- <version>1.3.2</version> <!– 使用最新版本或适合你项目的版本 –>--> <!-- </dependency>--> <!----> <dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>3.0.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.25.3:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.70.0:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>com.google.code.maven-replacer-plugin</groupId> <artifactId>replacer</artifactId> <version>1.5.3</version> <executions> <execution> <phase>generate-sources</phase> <goals> <goal>replace</goal> </goals> </execution> </executions> <configuration> <includes> <include>**/*.java</include> </includes> <replacements> <replacement> <token>javax.annotation.Generated</token> <value>jakarta.annotation.Generated</value> </replacement> </replacements> </configuration> </plugin> </plugins> </build> </project>
最新发布
07-16
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

青年IT男

您的打赏就是对我的肯定!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值