Created
November 6, 2020 11:56
-
-
Save kalaiselvan369/ab2e69dfc0dd9638da09ba065ecbc32d to your computer and use it in GitHub Desktop.
Overflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
sumOfTwoBytes() | |
} | |
/* | |
Why the answer is -16 when we expect the result in byte. Let's do that manually. We know that byte can store values | |
from -128 to 127 only. Here we want to store the 240 which is not possible. But how -16 is returned. | |
Binary value of 240 is 11110000 | |
Byte has 8 bits where each bit can store either 0 or 1. | |
Bit 7 6 5 4 3 2 1 0 | |
240 1 1 1 1 0 0 0 0 | |
But a caveat here is 7 th bit can store only +(plus) or -(minus) since it is a signed bit. So for the answer we have to consider | |
digits from 6 th bit only. | |
From 6 th bit the binary value is 1110000. If we want to know the magnitude of this binary value in computer memory we | |
have to perform 2's complement. | |
1's complement - inverting all the binary digits. | |
2's complement - adding 1 to the 1's complement result | |
Let's take 1's complement for 1110000 => 0001111 | |
Then 2's complement will be 0001111+1 => 0010000 | |
Now if we do binary to decimal conversion for 0010000 we will get 16. Since we have considered 7 th bit for binary value | |
of number 240 has signed bit and also 7 th bit hold's value 1 therefore it is -16. If we would have choose Int data type | |
we would have got extra bits to store overflow bits and our answer would have been correct. | |
1 => - sign | |
0 => + sign | |
Example: 3 bit have 8 patterns and it can store value range from -4 to 3 | |
000 =>0 | |
001 =>1 | |
010 =>2 | |
011 =>3 | |
100 =>-4 | |
101 =>-3 | |
110 =>-2 | |
111 =>-1 | |
*/ | |
fun sumOfTwoBytes() { | |
val a: Byte = 120 | |
val b: Byte = 120 | |
//val result: Byte = (a + b).toByte() // Error: answer will be -16 instead of 240 | |
// result variable takes Int as data type because Byte can store only values rangin from -128 to +127, | |
// compiler is so smart to do that. | |
val result = a + b | |
println(result) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment