eli5: Type casting

604 views

casting of larger variable to smaller variable results in the modulo of larger variable by the range of smaller variable.

I mean seriously explain it like I’m 5

In: Other

2 Answers

Anonymous 0 Comments

Lets forget binary and all that for first. Imagine you have two datatypes, one is an 8 digit decimal number the other is a 4 digit decimal number.

If you cast 12345678 into the 4 digit type it will just forget the first 4 digits. So the result is 5678.

That is the mathematical operation “modulo by the range of the 4 digit number”. So in this case Modulo 10000 because thats how many numbers a 4 digit decimal number can represent.

Modulo means “divide with remainder and return only the remainder”, so basically the part of the number that can’t be divided by 10000.

With binary exactly the same happens.

Anonymous 0 Comments

Say the larger type allows numbers from 0 to 9999 and the smaller only 0 to 99.

Then if the variable with large type has value 287, after type casting to the smaller type, it becomes 87. Other example: 345 becomes 45, 66 remains 66. Essentially the modulo means the amounts “over” the max value of the smaller type is lost.