Saturday, 15 February 2014

Why does the bitwise complement operator '~' behave differently in c# and java? -



Why does the bitwise complement operator '~' behave differently in c# and java? -

this question has reply here:

how bitwise complement (~) operator work? 9 answers

when execute code in c# , java, different output. in c#, got output 254 in java got output -2. why behave differently in term of output? want same output in java means 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 unsigned 8-bit integer value, i.e. range 0-255. in java, however, byte signed 8-bit integer value, i.e. range -128-127. -2 (signed) has same binary representation 254 (unsigned).

java c# operators byte

No comments:

Post a Comment