cs12dotnet8 icon indicating copy to clipboard operation
cs12dotnet8 copied to clipboard

Chapter 3, page 149

Open m-ngr opened this issue 1 year ago • 0 comments

Hi Mark, At page 149, the book says: "That is why when you have an integer too large to fit in a 32-bit integer, it becomes -1."

image

It's not always the case that the result of this type of casting is -1. When casting from a wider integer data type to a narrower integer data type, the most significant extra bits gets truncated and the least significant bits represents the result of the casting.

For example:

long x = 0b_101000101010001100100111010100101010;
int y = (int) x;

Console.WriteLine($"{x,64:B64} = {x}");
Console.WriteLine($"{y,64:B32} = {y}");

the result:

0000000000000000000000000000101000101010001100100111010100101010 = 43657622826
                                00101010001100100111010100101010 = 707949866

m-ngr avatar May 26 '24 17:05 m-ngr