When I execute the code in c# and java, I get different output.
In c#, got the output 254
but in java got the output -2
. Why does it behave differently in term of output? But I want the same output in java means I want output 254
.
In c# code:
static void Main(string[] args)
{
byte value = 1;
System.Console.WriteLine("Value after conversion {0}", (byte)(~value));
}
Output : 254
In Java code:
public static void main(String[] args) {
byte value = 1;
System.out.println((byte)(~value ));
}
Output : -2
In C# byte
denotes an unsigned 8-bit integer value, i.e. its range is 0-255. In Java, however, a byte is a signed 8-bit integer value, i.e. its range is -128-127. -2 (signed) has the same binary representation as 254 (unsigned).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With