简体   繁体   中英

How to get a unique value in Java that corresponds to uint32_t?

I have to generate a unique number in Java (for my machine on which the code is running) so that in C++ it can correspond to uint32_t . In general other C++ program should be able to read this unique number properly as uint32_t . I am sending this info in a byte array which other C++ program is deserializing it to get this number. I cannot change the datatype for this number in C++ program as of now.

  private static final AtomicInteger clientId = new AtomicInteger(0);

  // will be called many time when the program is running after initialization
  public static int getClientId() {
    int counter = clientId.incrementAndGet();
    return counter == Integer.MAX_VALUE ? 0 : counter;
  }

  // called only once at startup
  public static void setClientId(final int counter) {
    clientId.addAndGet(counter == Integer.MAX_VALUE ? 0 : counter);
  }

So I came up with above code. My setClientId method will be called only once during the application startup (initialization phase):

  • If this machine is running code for the first time, like it's a new machine then I will pass 0 to setClientId method.
  • If this machine was running code already but we restarted the server, then I will look up in a database what was the last saved value for this machine (as we are also saving this value in db every few minutes) and then I will pass that value to setClientId method.

And then later on when my program is running (after the initialization) it will keep calling getClientId method to give me the actual unique clientId.

Is this the right way of generating unique id for my machine which will be uint32_t for the C++ program? I am also making sure that if the value reaches Integer.MAX_VALUE then set it to 0 and start again. Do I have to do this? Or I can take negative values as well for this uint32_t ?

Basically, I want to generate a unique number for my machine that should always corresonds to uint32_t . If machine is a new machine, we can start with number 0 (bcoz when we look up the value for this machine the db, there won't be any value so we will start with 0) but if machine has already ran this code before, then start with what was the last saved value in the database.

Do I have to do this? Or I can take negative values as well for this uint32_t?

No, you don't have to, and you can take negative values as well. If you pass the Java integer to C++ bit-by-bit, the unit32_t in C++ is going to keep increasing even when the Java variable goes over the max value and becomes negative. That's the good thing about 2's complement signed integers, which is what Java uses to represent integral values.

Here's a 4-bit example illustrating what's happening:

Bits  Unsigned  Signed
0000         0       0
0001         1       1
0010         2       2
0011         3       3
0100         4       4
0101         5       5
0110         6       6
0111         7       7
1000         8      -8
1001         9      -7
1010        10      -6
1011        11      -5
1100        12      -4
1101        13      -3
1110        14      -2
1111        15      -1

Also see this answer: What happens when you increment an integer beyond its max value .

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