Khóa học algorithms

Bitwise NOT

0 phút đọc

The NOT bitwise operation inverts bits. A 000 becomes a 111. A 111 becomes a 000.

The NOT operator is often written as a tilde character ("~"):

python Copy
~ 0000 0101
= 1111 1010

When numbers are printed in base-10, the result of a NOT operation can be surprising. In particular, positive numbers can become negative and negative numbers can become positive. For example:

python Copy
  ~ 5  # gives -6

# At the bit level:
#   ~ 0000 0101  (5)
#   = 1111 1010  (-6)

This is because numbers are (usually) represented using two's complement, where the leftmost bit is actually negative. So flipping the leftmost bit usually flips the sign of the number.

Gợi ý câu hỏi phỏng vấn

Không có dữ liệu

Không có dữ liệu

Gợi ý bài viết
Không có dữ liệu

Không có dữ liệu

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào