Siva
Siva

Reputation: 1325

what does 16bit offset and 24 bit offset mean? how do i do such calculations using java

I have a spec which reads the column specifies a 16 bit offset to a structure and another column specifies a 24 bit offset to a structure. I am implementing the spec using java. I am not clear of what 16 bit offset /24 bit offset means and how to perform such operation in java.

Upvotes: 0

Views: 11953

Answers (3)

Bohemian
Bohemian

Reputation: 425318

It sounds like you have single piece of data that is segmented into bit series.

  • Bits 16-23 are the "16 bit offset"
  • Bits 24-? are the "24 bit offset"

The data is probably a int (32-bit signed integer) or a long (64-bit signed integer) with certain parts of the bit sequence allocated to storing separate smaller pieces of data.

One way to simply get at the values is to use a bit mask and right shift, like this:

int mask = 0xf00; // only bits 16-23 are 1
int data;
int value = data & mask; // zero other bits
value >>= 16; // shift the value down to the end

Upvotes: 4

Artefacto
Artefacto

Reputation: 97835

It probably means the boundary of the data starts 16 bits or 24 bits after the start of that structure.

How you access it depends on how you got access to the structure to begin with.

If it's just something you read into a byte array, some data stored offset 16 bits could be accessed by ignoring the first two elements of the array (2 byte = 8 bits * 2), or 3 for 24 bits. If it's an long or an int, it depends if you can use the >> and << shift operators.

Upvotes: 0

Joachim Sauer
Joachim Sauer

Reputation: 308239

An offset is a relative address in some stream and/or storage medium.

A 16bit offset is an offset that's stored in a 16 bit variable/slot.

So if some file format specification says that "the next field is the 16 bit offset" that means you must read the next 2 byte and treat it as a relative address.

What exactly that addresses depends on the specification: it might be bytes, it might be "entries" or anything else.

Note also, that Java doesn't have any built-in 24 bit data types, so you'll have to work around that using int, which has 32 bit.

Upvotes: 2

Related Questions