Question

I'm doing c type (u8, s16, u32,..) - to java conversion (just a class which contains boolean signed, and long value). And of course length for the number of bits.

So for example: u8 signed=false length=8;

However In converting the types to an int[]

There is this:

int[] ret = new int[length / 8];
for (int i = 0; i < ret.length; i++) {
  ret[ret.length - 1 - i] = (char) (0xff & (value >> ((ret.length - i - 1) * 8)));
}

For example value=7; length=8

what could 0xff & () accomplish? since

0xff is just 1111 1111 and 7 is 0000 0111 => result 0000 0111

Or is something else happening here?

Was it helpful?

Solution

The 0xFF selects the first 8 bits of an int. So if value is an int, you select the first 8 bits and thats the reason you need it: If your int containts more then 8 bit you can select them with this bytemask. The value >> stuff moves the interesting bits from the current position to the first 8 positions so the bytemask can select them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top