The following code:
private const uint FIRMWARE_DOWNLOAD_ADDRESS = 0x00001800;
public void someFunc(){
byte[] command = new byte[16];
command[11] = (byte)(FIRMWARE_DOWNLOAD_ADDRESS >> 24);
command[10] = (byte)(FIRMWARE_DOWNLOAD_ADDRESS >> 16);
command[9] = (byte)(FIRMWARE_DOWNLOAD_ADDRESS >> 8);
command[8] = (byte)(FIRMWARE_DOWNLOAD_ADDRESS); //error: Overflow in constant value computation
}
throws an error Overflow in constant value computation.
Why? From what I understand 0x00001800 <= 0xffffffff so there should be no overflow happening.
And why don't the other 3 lines throw an error? I tried to do:
command[8] = (byte)(FIRMWARE_DOWNLOAD_ADDRESS >>0);
thinking that the right shift operator was somehow checking for the overflow condition but this still gives the same error.
1 Answer 1
You get the error because the value you are trying to cast to a byte cannot be represented by a byte.
A byte's max value is 0x000000FF (or 255). But you are trying to cast 0x00001800 (or 6144). A byte simply cannot contain that value.
The remaining works fine since, after the bit shift, the value is small enough to be represented by a byte
FIRMWARE_DOWNLOAD_ADDRESS >> 24 = 0
FIRMWARE_DOWNLOAD_ADDRESS >> 16 = 0
FIRMWARE_DOWNLOAD_ADDRESS >> 8 = 24
It seems like you are thinking about an unsigned integer's max value, which is 0xFFFFFFFF (or 4294967295)