+ 7
Can you explain me this?
print(~5) # output=-6 but output should be -10 why it giving -6,can someone explain it.
7 Respuestas
+ 10
-6 is correct. ~ is a bitwise not. It flips 0's to 1's and 1's to 0's. It operates on the all the bits.
00000101
becomes
11111010
which is -6 in Two's compliment.
Usually you can figure that if you negate the number and subtract 1 that will be your answer or two's compliment.
-(x) - 1
+ 4
ChaoticDawg
I've actually been wondering how the flipped version is -x-1. Isn't 11111010 in decimal 250...?
+ 2
ChaoticDawg binary of 6 is =0110
but how binary 11111010 is 6 please tell me I don't know about it.
+ 2
This should help to answer your questions.
https://www.calvin.edu/academic/rit/webBook/chapter5/negative.htm
+ 2
LunarCoffee The left most digit(s) in binary is/are used to indicate whether a number is positive or negative. If the number is 0 it is positive and 1 if negative. You also need to think in the size of the total bits for the given type, not just how many you need to go to determine the given number. For instance if an int type for the given language is 32 bits, then even the number 1 or 0 is still 32 bits long. So a 1 would have 31 0's to the left of it. A -1 in two's compliment would be a 1 with 31 1's to the left of it. In two's compliment the first bit you count from is the first bit that is opposite the left most bit(s) then you will count each bit that is 1 to the right of that bit.
So for -6 represented as a type that uses a single byte, (11111010) all the 1's to the left of 010 indicate the number is negative. Then the first leftmost 0s place is counted (4) Then each 1 to the right of it (2). Giving us -(4+2).
Or another way (probably the correct way) of looking at it, is that you take the first bit to the left of the 1st 0 (8) and then subtract each 1 (2) to the right of it giving us -(8-2).
+ 2
xie
如果你有一些实际有用的答案,为什么不以有益的方式添加到讨论中,而不是创建一个伪造的帐户来制作一个伪造的帖子
顺便说一句,没有错,我只是用一种容易理解的方式来解释它
- 2
错啦