- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
>> 在我的测试中与 >> 没有区别。 -128 >> 4 = -8 正如预期的那样,但是-6ren">
我正在尝试右移以将某些位与字节隔离,所以我想要一个无符号移位,据我所知,"new"位应该为零。但是,我发现 >>> 在我的测试中与 >> 没有区别。 -128 >> 4 = -8 正如预期的那样,但是 -128 >>> 4 应该是 8 但我仍然得到 -8。
byte data = (byte)-128;
System.out.println((byte)(data >>> 4));
System.out.println((byte)(data >> 4));
感谢您的帮助。
最佳答案
无符号右移运算符确实在这段代码中进行了无符号右移;它只是因为来自 byte
的隐式转换而被隐藏至int
.说 Java 语言规范 (§15.19):
The operators
<<
(left shift),>>
(signed right shift), and>>>
(unsigned right shift) are called the shift operators. The left-hand operand of a shift operator is the value to be shifted; the right-hand operand specifies the shift distance. [...] Unary numeric promotion (§5.6.1) is performed on each operand separately.
一元数字提升表示(§5.6.1):
[...] if the operand is of compile-time type
byte
,short
, orchar
, it is promoted to a value of typeint
by a widening primitive conversion (§5.1.2).
所以你的代码被评估如下:
byte
值(value) -128
这是 >>>
的左操作数晋升为int
值(value) -128
,即 0b11111111111111111111111110000000
二进制。int
上完成的。值,结果为 268435448
二进制是0b00001111111111111111111111111000
.请注意,最左边的四位为零,正如您对无符号右移所期望的那样。(byte)
, 给出结果 -8
.使用 REPL:
> byte b = -128;
> int shifted = b >>> 4;
> shifted
268435448
> (byte) shifted
-8
对于你想要的行为,你可以使用& 0xFF
从 byte
进行“无符号”转换至int
:
> ((b & 0xFF) >>> 4)
8
关于Java 的无符号位移右 (>>>) 是有符号位移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60270884/
如果此答案为 Kotlin,我想转换代码:https://stackoverflow.com/a/5402769/2735398 我将其粘贴到 Intellij 中: private int deco
对于这种转变: System.out.println(0b10000000_00000000_00000000_00000001>>>32); 我得到这个输出值: -2147483647 对吗? 最佳
如果没有大量工作,我想要的可能是不可能的,但也许有人有解决方案。我有一个像下面这样的图(这当然是一个过于简单的例子),其中我的刻度标签彼此非常接近: dd <- data.frame(x=1:4, y
试过这个代码on Go playground : package main import ( "fmt" ) func main() { log2Dim := uint32(9)
我希望在用户滚动时获取 UIScrollView 的位移,每次滚动时只需要位移。使用这个方法 -(void)scrollViewWillEndDragging:(UIScrollView *)scro
我正在检查我在装有 iOs 5.1 的 Ipad 中的应用程序当我点击 textarea 时,光标会跳到书写线上方两行。虽然焦点位于正确的位置,但光标的视觉外观出现在焦点线上方两行。对此有任何建议。
你如何获得像 -10 这样的数字?从这些移位练习题中? 据我了解X*32可以写成 x> 1 = 0001 0111 >> 1 = 000 1011 = 11 //right bit-shift by
我正在查看适用于 Linux 的开源 AMD GPU 驱动程序。我注意到一些我以前没有见过的东西,我想知道目的。在sid.h文件的第1441行,有一系列的定义是整数被左移0位。这不就是对原始整数进行操
这个网站 localhotchat.com 有文字从红色栏流出。我试过调整它,但它似乎并没有消失。 主页(和内部)上的 14pt 文本片段应该在 red_colored_strip 内,但它不是,并且
问题#1 在 Java 中,移位是否比使用单个语句将 移位相同数的代价高出数倍? 例如,是 int x = 5; x = x << 16; 比快 int x = 5; for (int i=0; i<
我有三个 Div,周围的 Div 是“MainDiv”,里面的 Top Div 是“UpperDiv”下一个是“FooterDiv”。 在 Mozilla 中一切正常,但在 Chrome 中“Foot
所以,我正在阅读一本关于 Go 的书(Ivo Balbaert 的 The Way to Go),其中有一个代码示例: const hardEight = (1 > 97 因为我没有在这台机器上安装
Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。 下面就讲一下Tweene Animations。 主要类:
我有 UINavigationItem,我想向它添加 UISearchBar。我是这样做的: let searchBar = UISearchBar(frame: CGRectMake(0, 0, s
本文实例讲述了Android动画之渐变动画(Tween Animation)。分享给大家供大家参考,具体如下: Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的
我在 C# 中使用 openCV 库中的 Lucas Kanade 光流算法;有一系列帧,我想在每两个帧中找出什么是光流并将其显示在图片框中。 我可以从以下函数中获取 velX 和 velY: Emg
我是一名优秀的程序员,十分优秀!