[英]Converting java code to c++
How do I convert the below piece of java code to C++. 如何将下面的Java代码转换为C ++。 I know I can write
typedef unsigned char byte
so that is taken care of, but I don't understand what the |=
and <<=
are meant for. 我知道我可以写
typedef unsigned char byte
这样就可以了,但是我不明白|=
和<<=
是什么意思。 And how does one replace final
以及如何替代
final
public static final long unsignedIntToLong(byte[] b) {
long l = 0;
l |= b[0] & 0xFF;
l <<= 8;
(l >>> 4) & 0x0F;
How do I test all this in C++ - are there some unit tests I can run as I go about the conversion. 如何在C ++中测试所有这些-在进行转换时是否可以运行一些单元测试。
First thing, |=
is a compound bitwise OR assignment. 首先,
|=
是复合的按位或分配。 a |= b
is equivalent to a = a | b
a |= b
等效于a = a | b
a = a | b
, where each resulting bit will be set if either that bit in a
or b
is set (or both). a = a | b
,如果设置了a
或b
中的a
位(或同时设置了这两个位),则将设置每个结果位。
Here's a truth table that is applied to each bit: 这是应用于每个位的真值表:
a | b | result
--------------
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
Secondly, <<=
is the same, but instead of a bitwise or, it's a bit shift to the left. 其次,
<<=
是相同的,但是不是按位或,而是向左移一点。 ALl existing bits are moved left by that amount, and the right is padded with 0s. AL1现有位向左移动该数量,向右填充0。
101 << 1 == 1010
110 << 2 == 11000
final
is the same as C++'s const
by the variable definition. final
通过变量定义与C ++的const
相同。 If, however, you want to prevent a function from being overriden, you may tag final
onto the end of the function header if the function is also a virtual function (which it would need to be in order to be overriden in the first place). 但是,如果您想防止某个函数被覆盖,则如果该函数也是一个虚拟函数,则可以将
final
标签标记在函数头的末尾(首先需要重写该函数) 。 This only applies to C++11, though. 不过,这仅适用于C ++ 11。 Here's an example of what I mean.
这是我的意思的例子 。
Finally, >>>
is called the unsigned right shift
operator in Java. 最后,
>>>
在Java中称为unsigned right shift
运算符。 Normally, >>
will shift the bits, but leave the leftmost bit intact as to preserve the sign of the number. 通常,
>>
将移位这些位,但保留最左边的位不变以保留数字的符号。 Sometimes that might not be what you want. 有时这可能不是您想要的。
>>>
will put a 0 there all the time, instead of assuming that the sign is important. >>>
始终将0放置在那里,而不是假定符号很重要。
In C++, however, signed
is an actuality that is part of the variable's type. 但是,在C ++中,
signed
是一种现实,它是变量类型的一部分。 If a variable is signed, >>
will shift right as Java does, but if the variable is unsigned, it will act like the unsigned right shift ( >>>
) operator in Java. 如果一个变量是有符号的,
>>
将会像Java一样右移,但是如果该变量是无符号的,它将像Java中的无符号的右移( >>>
)运算符一样起作用。 Hence, C++ has only the need for >>
, as it can deduce which to do. 因此,C ++只需要
>>
,因为它可以推断出该做什么。
|= is the same in both languages: bit-wise OR applied to the lhs variable, just like +=. | =在两种语言中都相同:按位或应用于lhs变量,就像+ =一样。 Same with <<=;
与<< =相同; it's the shift bits left operator.
是左移运算符。
Unsigned long could be tricky; 无符号长可能很棘手。 no such thing in Java.
Java中没有这样的东西。
There's CppUnit. 有CppUnit。 Try using that.
尝试使用它。
There is no straight answer on how to write a final class in C++. 关于如何用C ++编写最终类没有直接的答案。 Google will show you a lot of examples though.
Google会向您展示很多示例。 For example a private constuctor or a frend class.
例如,私有构造函数或朋友类。
| | is the OR operator.
是OR运算符。 So for example 0x10 |
例如0x10 | 0x10 = 0x11.
0x10 = 0x11。
<< is the bitshift operator. <<是位移位运算符。 So for example 0b1 << 0b1 = 0b10, 10 << 2 = 0b1000 and so on.
因此,例如0b1 << 0b1 = 0b10,10 << 2 = 0b1000,依此类推。 Shifting by 1 multiplies your value by 2.
移位1乘以2。
For example: 例如:
class Converter{
public:
static Converter * createInstance()
{
return new Converter;
}
long unsignedIntToLong(unsigned char *b){
long l = 0;
l |= b[0] & 0xFF;
l <<= 8;
//..
return l;
}
private:
Converter(){
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.