How does arduino uno cast from long to int?

Does it truncate the bits? (Does it just delete the 16 most left bits?)


Yes, it just blindly chops off the upper 16 bits. That means that the sign can change between the two.

For instance, 123456789 gets truncated to -13035 and -123456789 truncates to 13035.

To see that in more detail, look at the binary. 123456789 in binary is:

0000 0111 0101 1011 1100 1101 0001 0101

Truncate that at 16 bits and you get:

1100 1101 0001 0101

And of course the high (sign) bit is set, so the value is negative.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.