+ 2

How the tilde calculation happened??

int x=10; int y = - 10; System.out.println(~x); System.out.println(~y); Output: -11 9 How the tilde calcution happened?? Can you elaborate it please!!!

6th Jun 2018, 5:11 PM
Ferdous Rayhan
Ferdous Rayhan - avatar
12 RĂ©ponses
+ 2
basically ~10 in binary is (for a one byte datatype) ~00001010=11110101 which is -11 in decimal but with twos complement its always ~x=-x-1, so yes
6th Jun 2018, 5:58 PM
Max
Max - avatar
+ 3
yes but if you store it in a variable that is for example 1 byte long it is stored as 0000 1010 because the other 4 bits are filled up with zeros. if you store it in a 2 byte vaiable it is stored as 0000 0000 0000 1010. because of how the twos complement works out in both cases inverting gives -11. just read the wikipedia article on twos complement
6th Jun 2018, 6:10 PM
Max
Max - avatar
+ 2
every bit that was 1 is 0 and every bit that is 0 is 1 after ~. because of the way twos complement works ~x = -x -1
6th Jun 2018, 5:34 PM
Max
Max - avatar
+ 2
Ferdous Rayhan it should be twos complement, not ones complement. in ones complement ~x=-x but in your examples ~x=-x-1 all modern processors use twos complement, because it is the most efficient to implement and because you don‘t have to deal with the negative zero problem so you will almost never stuble across ones complement
7th Jun 2018, 2:45 PM
Max
Max - avatar
+ 2
10=0000 1010 -> 11110101 which would be -10 in ones complement. but -11 in twos complement. you have them mixed up in ones complement ~x=-x but in twos complement ~x=-x-1 thats why in ones complement you have two ways to write zero(0 and ~0=-0) but in twos complement only one way
7th Jun 2018, 3:09 PM
Max
Max - avatar
+ 1
~x= - x-1 is it the rules?? ~10=-10-1=-11 ~(-10)= -(-10)-1=9..like that??
6th Jun 2018, 5:56 PM
Ferdous Rayhan
Ferdous Rayhan - avatar
+ 1
with your theory ~x=-x-1 its easy but i want to learn in deep .. 10 in binary is 1010..i convert it online..can you explain pi to pi please ..like 1st we got the binary of 10 which is 1010 then we complement it..which is 0101 then for two complement we add 1 with it..then we convert it to decimal..is it correct?? but i did that in converter..it is not right!!
6th Jun 2018, 6:08 PM
Ferdous Rayhan
Ferdous Rayhan - avatar
+ 1
hey...its just ones complement ...i just invert the bytes and convert to the decimal..and it results same as the compiler...not 2s complement...now..after study i know that 2's complement is nothing but adding one with the ones complement....thanks to you a lot...can you say me..when ill need 2s complement and where to use these ones and twos complement...means when ill need to use those or where to use..thank u again
7th Jun 2018, 2:40 PM
Ferdous Rayhan
Ferdous Rayhan - avatar
+ 1
Max i just calculated..look...10=00001010=11110101= -11(~x= -x -1 : ones complement) & 10 =00001010=11110101 +1=11110110= -10(~x=-x :twos complement)....in this way i did same with the int - 10 and 9 ....they were same...and it matched with the compiler...i just converted those signed 8 bit int in mathisfun(dot)com...everything is looking fine now except i got confused hearing you ..can you please check it once more for me please!!!
7th Jun 2018, 3:05 PM
Ferdous Rayhan
Ferdous Rayhan - avatar
+ 1
https://www.mathsisfun.com/binary-decimal-hexadecimal-converter.html here...i put 11110101 and it shows the decimal is - 11....and 11110110 = -10;;;
7th Jun 2018, 3:15 PM
Ferdous Rayhan
Ferdous Rayhan - avatar
+ 1
Max i need a reply...where the mistake
7th Jun 2018, 3:19 PM
Ferdous Rayhan
Ferdous Rayhan - avatar
+ 1
yeah of course, it uses twos complement like everything else. both of the numbers you are inputing are interpreted as twos complement -11 is 11110101 in twos complement and -10 is 11110110 in twos complement -10 would be 11110101 in ones complement and -11 would be 11110100 in ones complement
7th Jun 2018, 3:19 PM
Max
Max - avatar