I'm trying to build an iOS app based on an existing Android app and have to convert a function involving bit-shifting from Java to Swift. This is the original function in Java:
public static void BUF_U32_BE(byte[] d, int loc, int val) {
d[loc] = (byte)(val >> 24);
d[loc+1] = (byte)(val >> 16);
d[loc+2] = (byte)(val >> 8);
d[loc+3] = (byte)(val & 0xFF);
}
This was my attempt at converting the function to Swift:
func BUF_U32_BE(byteArray: [UInt8], loc: Int, val: Int) -> [UInt8]{
var d = byteArray;
d[loc] = UInt8((val >> 24));
d[loc+1] = UInt8((val >> 16));
d[loc+2] = UInt8((val >> 8));
d[loc+3] = UInt8((val & 0xFF));
return d;
}
When running the Swift version, I get the error:
Fatal Error: Not enough bits to represent the passed value
This is the line that produces the error:
d[loc+1] = UInt8((val >> 16));
What could be causing this issue and what should I do to fix it?