Lets Break the algo into pieces before trying to understand
the algo is broken into pieces using bodmas (bracket open ,divide ,multiply ,add, subtract )
and/or Operator precedence
lets take the uncasted readB first
static int readB(byte[] bytes, int i) {
return ((bytes[i] & 255) << 8)
| (bytes[i + 1] & 255);
}
prototype of function says it takes an array of bytes and an integer does some thing and returns an int back
static int readB(byte[] bytes, int i)
body of the function (where it does the aforementioned something
return ((bytes[i] & 255) << 8) | (bytes[i + 1] & 255);
there are two bracketed expression one of which has a child
1. ((bytes[i] & 255) << 8)
I. (bytes[i] & 255)
3. (bytes[i + 1] & 255)
breaking the child expression apart it has three components a constant 255 and two variables
which are arguments or inputs provided to the function
the byte array bytes[] and
the integer i
since i is an int the array can range from 0 to 2^31 -1
( think what will happen if you provide a null array or
an array with just 1 value or
an array with 2^31-1 values
or int >= array size
(bounds checking
this link has the following code check the results and see the thrown exception
{
static byte foo[] = {1,2,3,4,5,6,7,8 };
static int readB(byte[] bytes, int i)
{
return ((bytes[i] & 255) << 8) | (bytes[i + 1] & 255);
}
public static void main(String args[])
{
for(int i = 0; i < foo.length ; i++ )
{
System.out.printf("bounds check %d %d %d\n" , i , foo[i] , readB(foo,i));
}
}
}
result of running the code
Compilation time: 1.05 sec, absolute running time: 0.22 sec,
cpu time: 0.15 sec, memory peak: 18 Mb, absolute service time: 1,27 sec
Error(s), warning(s):
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 8
at Rextester.readB(source.java:12)
at Rextester.main(source.java:18)
bounds check 0 1 258
bounds check 1 2 515
bounds check 2 3 772
bounds check 3 4 1029
bounds check 4 5 1286
bounds check 5 6 1543
bounds check 6 7 1800
)
bytes[i] & 255
this actually is superfluous operation
the bytes[i] array is already of BYTE type so the values can never exceed 255 and there is no point stripping the rest
this would only make sense if the type is > BYTE like WORD foo[] which can hold anything from 0x0000 to 0xffff or int foo[] which can hold anything from 0x00000000 to 0xffffffff so stripping the hibyte and highwords may make sense .
in this specific code we can safely ignore this
so basically it takes two bytes from the given position and folds them into a bigger type
so readA returns a SHORT and readB returns and INT type
ie readA takes two bytes from a give position and folds it into anything between
0x0000 and 0xffff
readB takes two bytes from a given position and folds into anything between
0x00000000 and 0xffffffff (actually it is same as 0x00000 and 0xffff if not for size of type calculation or pointer arithmetic's )
the third readC now should be clear enough for you ittakes 4 bytes and folds it into anything between 0x00000000 and 0xffffffff (this function utilizes the whole range not unlike the readA() / readB()
see the shifts 24,16,8 which puts
byte[ position 1] at 0xAA-??????
byte[ position 2] at 0xAA-BB-????
byte [position 3] at 0xAA-BB-CC-?? and
byte [position 4] at 0xAA-BB-CC-DD
and returns back 0xAABBCCDD
readA
returnsshort
created as concatenation ofbytes[i]
andbytes[i+1]
(i.e. ifbytes[i]=00000010
andbytes[i+1]=10000011
, it will return10100000011
).readB
returns the same except that will be of typeint
, whilereadC
returns anint
representing concatenation ofbytes[i]
,bytes[i+1]
,bytes[i+2]
andbytes[i+3]
. – bart1e Aug 25 '19 at 10:44