python - bit-wise operation unary ~ (invert) -


मैं ~ ऑपरेटर द्वारा थोड़ा भ्रमित हूँ। कोड नीचे जाता है:

  a = 1 ~ a2 बी = 15 ~ बी # -16   

कैसे ~ काम करो?

मैंने सोचा, ~ a कुछ ऐसा होगा:

  0001 = एक 1110 = ~ a  < / प्री> 

क्यों नहीं?

आप वास्तव में सही हैं यह पूर्णांक प्रतिनिधित्व का एक विरूपणिकता है।

16 बिट्स में, 1 को 0000 0000 0000 0001 के रूप में दर्शाया गया है। उल्टे, आपको 1111 1111 1111 1110 मिलता है, जो कि -2 है इसी प्रकार, 15 0000 0000 0000 1111 है। उल्टे, आपको 1111 1111 1111 0000 मिलता है, जो -16 है।

सामान्य में, ~ n = -n - 1 < / div>

Comments

Popular posts from this blog

mysql - BLOB/TEXT column 'value' used in key specification without a key length -

ruby on rails - Convert a Activerecord result with has_one associations into a single hash per record -

javascript - Read complete data from stream from onMessage -