- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我遇到了一个有趣的场景,在使用位移运算符时。如果第二个操作数是负数,移位运算是如何进行的? .
即 a << b ,“<<”将 a 中的位模式向左移动 b 位。但是如果 b 是负数,它不应该是运行时的错误吗?
我能够成功运行以下代码,但我不明白它是如何工作的?
public static void bitwiseleftShift(char testChar)
{
int val=testChar-'a';
int result= 1<<val;
System.out.println("bit wise shift of 1 with val="+val+" is "+result);
}
输入
bitwiseleftShift('A');// ASCII 65
bitwiseleftShift('0'); // ASCII 48
结果
bit wise shift of 1 with val=-32 is 1
bit wise shift of 1 with val=-49 is 32768
“a”的 ASCII 是 97。谁能帮我理解这是如何工作的?
最佳答案
But if b is neagtive, shouldn't it be an error at runtime?
不符合 Java 语言规范,section 15.19 :
If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (§15.22.1) with the mask value 0x1f (0b11111). The shift distance actually used is therefore always in the range 0 to 31, inclusive.
因此 -32 的偏移实际上最终变成了 0 的偏移,而 -49 的偏移实际上最终变成了 15 的偏移 - 因此您看到了结果。
关于java - 按负数移位的位移位运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15603327/
赏金:对于提供代码以使此子程序与负数一起工作的任何人,+50 信誉点。 我编写了一个 MIPS 程序来将华氏温度转换为摄氏温度。它打开自己的输出窗口(即 UART)并正确显示摄氏值。它在从 C 调用到
我得到了以下代码 # On va convertir en somme monétaire # We convert our integer into a way to read money
我得到了以下代码 # On va convertir en somme monétaire # We convert our integer into a way to read money
我使用以下 RegEx 基本上过滤掉任何文本,并接受数字 + 运算符。 ([-+]?[0-9]*\.?[0-9]+[\/\+\-\*])+([-+]?[0-9]*\.?[0-9]+) 所以它抓取 1+
我有一个查询,它计算我在查询中使用 union all 的平均值,以便获取我最终使用 max 函数的数据。 当联合返回结果时,如下所示:- col 1 col2 1 0
我有这样一个类: public class SpiralGenerator implements Iterator> { private void generate(int pos, E...
A = numpy.matrix([[36, 34, 26], [18, 44, 1], [11, 31, 41]]) X1 = numpy.matrix([[462
我有一个应用程序,其中有一个显示硬币 00 的 TextView ,一个按钮显示奖励视频广告,为用户提供 10 个硬币,还有一个购买按钮,将硬币减少 30 个。现在,当用户有 30 个硬币时,单击购买
话不多少,直接附上代码实例,仅供参考 ? 1
我有一系列正数和负数,我想将每个数字的绝对值增加一个,同时仍保持正数/负数。0.2 -> 1.2-0.3 -> -1.3我怎样才能做到这一点? 最佳答案 让我们尝试使用numpysign s=pd.S
我有这段代码,只允许在 keypress() 的输入字段中输入数字 if (e.which != 8 && e.which != 0 && (e.which 57)) { return fa
我试图用“-1”作为所有值填充二维数组。我正在使用的代码是: int c [] []=new int[4][4]; Arrays.fill(c,-1) 这会引发以下错误: Exception in t
在学校作业中,我们应该编写一个程序,该程序接受一个数字并将其分为三个部分:1. 检查数字是正数还是负数2. 整数(大小)3.小数部分 要求是应该有一个自己的函数,名为separate,具有输入和输出参
有没有什么方法可以在 C# 中执行整数除法(没有 float 或小数,我需要保持这个非常快)来向下舍入数字? 默认除法只是丢弃分数参数。考虑: 1 / 2 = 0 // That is correc
我正在使用 matplotlib 为报告生成图表,并指定我自己的样式表来指定文本格式以符合报告的指定文档样式。在我的 .mplstyle 样式表中,我按如下方式指定字体系列: font.family
在 C++11 中,如果我们尝试使用全局运算符 new 分配负大小的数组,它会抛出 std::bad_array_new_length,但是 C++98/C++03 呢?是 UB 还是会抛出 std:
我试过 scanf("%u",&number) 并且我输入了负数问题是当我 printf("%d",number) 我得到负数。我认为这会阻止我读取负数。scanf("%d",&number) 和 s
我的任务是解释一些看似奇怪的C代码行为(在x86上运行)。我可以轻松完成所有其他工作,但是这确实让我感到困惑。 代码段1输出-2147483648 int a = 0x80000000; int
js有问题吗? if("hello".indexOf("world")) { // I forgot to add > -1 here console.log("hello world");
我正在尝试使用 Antlr 4 设置一个简单的计算器。 语法: grammar calcGrammar; input : expression EOF; expression : MINUS
我是一名优秀的程序员,十分优秀!