Tehleel Mir
Tehleel Mir

Reputation: 853

How to calculate range of any datatype while casting it

For example, a question in my paper came like this

in java: byte's range is -128 to 127

byte x = (byte) 300;

what will be the value of x?

How can I calculate this thing on paper?

Upvotes: 0

Views: 159

Answers (2)

Bohemian
Bohemian

Reputation: 425208

You need to keep only the lowest 8 bits, then interpret that as a 2's compliment signed byte.

If using code, you would typically perform a bit-wise AND with 255 (being the value when the lower 8 bits of an int are all 1), ie 300 & 255, however to calculate this on paper, divide the number by 256 and keep the remainder (in code this would be the modulo operator, ie 300 % 256) which is your answer, but subtract 256 from the remainder if it's over 127.

In this case, because 300 happens to be in the range 256 to 256 + 127 (ie 383), you can just do 300 - 256 = 44 and you have your answer.

Upvotes: 2

Pieter12345
Pieter12345

Reputation: 1789

Lets for simplicity look at unsigned bytes (value 0-255) and arbitrary positive integers. When calculating what that integer would be when assigned to a byte, you have to see what the value of that integer is modulo 256. This means that you have to subtract 256 until your number is less than 256. In your example the integer is 300, and 300 modulo 256 is 44, so 44 would be assigned to an unsigned byte.

Upvotes: 0

Related Questions