0

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.

asked Mar 7, 2016 at 17:00
0

1 Answer 1

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)

answered Mar 7, 2016 at 17:17
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! So how would I go about programatically getting the last 8 bits of my constant value?
Doh! Use the binary AND operator with 0xFF!! Thanks again!
You found the answer before I could write my comment. :) You're welcome.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.