1

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.

4
  • 1
    This code doesn't do anything; there's no return statement. Commented Jun 3, 2012 at 23:16
  • And |= and <<= are identical in Java and C++, and the 'final' in 'static final long <method>' does precisely nothing. There is in fact no question here. Commented Jun 4, 2012 at 4:44
  • This code is more or less useless in C++. C++ already has a built-in unsigned int so there is no need to use an array of bytes. if you have a variable unsigned int x; and you need to convert it to long, you just do: long y = (long)x;, which is much simpler. Commented Jun 4, 2012 at 13:22
  • got it. This program accepts a data feed that is in different formats. this program is almost meant to serve the purpose of a hex editor. Commented Jun 4, 2012 at 14:44

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

9 Comments

final is not the same as const in this context.
+1, you can't restrict C++ classes not to be extended in any way.
@OliCharlesworth, oops, I didn't notice that. I was a bit still stuck on the other two. Thanks for pointing it out.
@OliCharlesworth, I've changed the final bit now (no pun intended). Please don't tell me they removed that like they did with extern templates :p
@chris, I just added an extra line of code, is that meant to be a bitwise >. can you post an example of how that would work. thanks,
|
3

|= 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.

Comments

0

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(){

    }
};

1 Comment

@OliCharlesworth, perhaps you could show some sample code on how the final will work. My code posted is not complete as I am still trying to understand what the different pieces are doing. This code is meant to decode a data feed that is not in ASCII format

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.