-9

I have little unusual problem to solve. I need some hint or links to get started with. I have queue with 10 data slots. Once the queue is full I need to send it to a server. However, along with that data, I also send start and end sequence number. Now, this numbers must be unique and in increment order. So, for the first send, start = 1, and end = 10. On second send, it would be start = 11, end = 20, and so on. Once the data from the queue is send, new entries will be recorded from the index 0 in the queue.

How do I solve this efficiently ?

Arjun Patel
  • 345
  • 6
  • 22
  • 3
    Are you sure those are the correct start and end numbers for a queue of 10 items? Also, what is preventing you from just storing the last end number and then incrementing? – Nathaniel Ford Jun 13 '12 at 20:18
  • I have completed the queue coding and etc. Just not sure how to generate incremental numbers. Because, the device I am working with is 16 bits. so I am not sure what would happen after 2^16 ? – Arjun Patel Jun 13 '12 at 20:23
  • 1
    @ArjunPatel sounds like a completely different problem than what you asked. – Kevin DiTraglia Jun 13 '12 at 20:24
  • @KDiTraglia yes, that is why I asked for some hints. I am thinking of wrapping start and end sequence along with another int, so that I will never run out of unique numbers. – Arjun Patel Jun 13 '12 at 20:29
  • 3
    how likely is it that you're going to be sending more than 65535 data slots? Also, it seems from your comments that your actual question is a lot more specific than the question you wrote. I would suggest that you go into more detail in the original post. – Ben Barden Jun 13 '12 at 20:37
  • There is a lot of context missing here. For example, what does *Java* have to do with a 16-bit device? – thkala Jun 13 '12 at 20:41
  • @thkala if the device is 16-bit then largest supported int would be 65535. – Arjun Patel Jun 13 '12 at 20:45
  • @Ben Barden after 6553th rotation, server will send error. Because after 65535, my start sequence will be rolled to 0. – Arjun Patel Jun 13 '12 at 20:47
  • @ArjunPatel: I know *that* (well, actually I don't - the largest 16-bit signed `int` is 32767). What I can't see is the relation between Java and 16-bit devices. Are you using a Java program to control such a device? – thkala Jun 13 '12 at 20:48
  • Yes, it is a proto board, which supports JAVA execution with KVM. – Arjun Patel Jun 13 '12 at 20:50
  • 1
    @ArjunPatel: As I said, there is a lot of context missing here. Perhaps you should edit your question to include the whole story... – thkala Jun 13 '12 at 21:03

1 Answers1

2

(There is a lot of context missing from your question, so this is mostly a shot in the dark...)

Since any 16-bit number can fit in a Java int primitive, you can:

  • Convert a Java int to a 16-bit number by ANDing the number with a suitable bitwise mask:

    i16 = i32 & 0xffff
    

    WARNING: This conversion is lossy and not easily reversible.

  • Convert a 16-bit number to a 32-bit int by keeping a separate epoch int that is incremented by one on each roll-over:

    if (previous16 > current16)
        epoch += 1;
    
    current32 = (epoch << 16) | current16;
    

    I do not think that it can get much more efficient than that in Java. Not to mention that any CPU that can run Java would normally run circles around any 16-bit processor, except perhaps for some DSPs...

A couple of related concerns:

  • Beware of signed/unsigned conversions: Java does not have unsigned types, which may complicate things, depending on what exactly your are doing.

  • Please note that, according to the JLS, the byte, char and short primitive types are implicitly converted to int for all operations. Whether they are actually narrower than 32-bits when stored in memory is implementation specific. And yes, that makes the short type pretty much useless...

thkala
  • 84,049
  • 23
  • 157
  • 201