How to use the negation operator in python

WBOY
Release: 2023-04-30 08:22:14
forward
3215 people have browsed it

    Python negation operator

    ~: Bitwise negation operator: negate each binary bit of the data, that is, turn 1 into 0, change 0 to 1.

    ~x is similar to -x-1

    1, calculation basis

    • ①The original code, inverse code and complement code of positive numbers are all The same

    • ②The complement of a negative number is that the sign bit remains unchanged, the remaining bits are inverted and 1 is added

    • ③The complement of the original code is found : Negate, 1

    • ④ Complement to find the original code: Negate, 1

    • ##⑤ Negative numbers are stored in complement

    • ⑥The inversion operation is performed on the original code

    • ⑦Sign bit, 1 represents a negative number, 0 represents a positive number

    2. Example

    ~(-2)

    First of all, you need to know the binary form of -2 in the computer. From ⑤ and ②, we can know:

    • -2 Original code: 1000 0000 0000 0010

    • -2 Negative code: 1111 1111 1111 1101 #The sign bit remains unchanged, please negate it

    • -2's complement: 1111 1111 1111 1110 #Complement code = inverse code + 1

    1111 1111 1111 1110 is the prototype of -2 in the computer

    Now start to negate it, the sign bit remains unchanged,

    bitwise negation: 0000 0000 0000 0001

    is obviously a positive number , its original code is itself, so the output is 1.

    ~2

    2 Original code, inverse code, complement code: 0000 0000 0000 0010

    Bitwise negation: 1111 1111 1111 1101

    What should I do if this is a negative number? Can I ask directly?

    Obviously this is not possible, ⑥ said: the negation operation calculation result is performed on the original code, ⑤ also said: negative numbers are stored in complement code

    1111 1111 1111 1101 It is the complement of 0000 0000 0000 0010, but what we want is the result, not the prototype in the computer,

    is just right, ④ complement to find the original code: negate, 1

    • Negate: 1000 0000 0000 0010

    • Add 1: 1000 0000 0000 0011

    So, we get the original code, you can calculate the result

    1000 0000 0000 0011 = -3

    In short, inversion is very simple, but you must pay attention to whether the inversion is the original code and whether the result can be calculated directly

    Python numerical inversion problem~

    In python, using the inversion symbol~ does not get the result we imagined. To sum up the rules, it is (the inverted value) = - (Value before inversion) -1

    Let’s analyze it

    For example, if 1 is inverted, the 8-bit binary representation of 1 is: 0000 0001, and the result after inversion is: 1111 1110, for computers, binary numbers starting with 1 represent negative numbers, so what exactly does 1111 1110 represent as a negative value? If you want to know this value, you can find its complement, that is, first take the complement: 0000 0001, Add 1:0000 0010, indicating that the original code of 1111 1110 is 0000 0010, which is a positive number 2, so 1111 1110 represents a negative number -2

    Here is another example

    Yes- 2 is inverted. The binary representation of -2 is the complement of 2, that is, 0000 0010 > 1111 1101 > 1111 1110. The binary representation of -2 is 1111 1110. Inverting it gives 0000 0001. Obviously, after inverting The value of is 1, so ~(-2)=1, ~1=-2

    The summary is: (value after negation) = -(value before negation)-1

    The above is the detailed content of How to use the negation operator in python. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:yisu.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!