简体   繁体   中英

Converting java code to c++

How do I convert the below piece of java code to 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. And how does one replace 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.

First thing, |= is a compound bitwise OR assignment. a |= b is equivalent to 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).

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.

101 << 1 == 1010
110 << 2 == 11000

final is the same as C++'s const by the variable definition. 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). This only applies to C++11, though. Here's an example of what I mean.

Finally, >>> is called the unsigned right shift operator in Java. 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.

In C++, however, signed is an actuality that is part of the variable's type. 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. Hence, C++ has only the need for >> , as it can deduce which to do.

|= is the same in both languages: bit-wise OR applied to the lhs variable, just like +=. Same with <<=; it's the shift bits left operator.

Unsigned long could be tricky; no such thing in Java.

There's CppUnit. Try using that.

There is no straight answer on how to write a final class in C++. Google will show you a lot of examples though. For example a private constuctor or a frend class.

| is the OR operator. So for example 0x10 | 0x10 = 0x11.

<< is the bitshift operator. So for example 0b1 << 0b1 = 0b10, 10 << 2 = 0b1000 and so on. Shifting by 1 multiplies your value by 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(){

    }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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