繁体   English   中英

使用按位运算符和无符号int的C ++到Java的移植代码

[英]C++ to Java porting code with bitwise operators and unsigned int

我需要将C ++代码转换为Java。 我有两个我担心的问题。

1)将'unsigned int'从C ++转换为Java'long'。 我选择使用较长时间来增加存储容量。

2)使用按位运算符,特别是| 和<<。 鉴于我已将unsigned int值转换为long,这对这些运算符会不会有不好的影响? 例如在C ++中:

unsigned int a;
unsigned int b;
unsigned int c;

a | (b<<c)

在Java中可以这样做吗?

long a, b, c;

a | (b<<c)

请让我知道您在执行这些操作时可能遇到的任何问题。

谢谢

long是用Java签名的。

long数据类型是64位带符号的二进制补码整数。 最小值为-9,223,372,036,854,775,808,最大值为9,223,372,036,854,775,807(含)。

来自C ++的unsigned int是一个字长(在32位计算机上为32位)。 它的范围是0到4,294,967,295。

它应该工作。 记住Java long是64位的。 唯一真正的区别是Java整数是带符号的。

运算符的行为应与无符号的行为相同: +-==&| ^<<

这些将更改行为: */%<

使用>>>而不是>>来对/2**k进行无符号解释(所压入的位是0 ,不是MSB副本)。

       Bitwise operations

  It's important to remember that the unsigned keyword affects 
  the interpretation, not the representation of a number. In other 
  words, in cases where we aren't interpreting a value arithmetically— so-called
  bitwise operations such as AND, OR, XOR— it makes essentially no 
  difference whether a value is marked as "signed" or "unsigned"

Unsigned int等效于Java中的long。 因此,这有所作为。 欲了解更多信息,请点击这里

我相信您所做的是安全的,并且应该可以在Java中正常工作。 如显示的那样,按位操作应按预期方式工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM