Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialMark Chesney
11,747 Pointsbitwise not operator: tilde vs minus sign (~ vs -)
I didn't notice there being any difference between ~ and - used for negating an index. For example:
no_referrals_index = users.referral_count < 1
~no_referrals_index
users[~no_referrals_index]
-no_referrals_index
users[-no_referrals_index]
Just an observation. Am I wrong?
2 Answers
Mark Chesney
11,747 PointsYes, I'm coming from the context of this exercise, using these operators on pandas series, as inputs into a data frame, rather than the discrete math examples
Steven Parker
231,271 PointsYour example doesn't show what the values being used are, but there should be a difference.
Since integers are stored in "two's complement notation", -0 (minus 0) is still 0, but ~0 (bitwise not 0) is -1. Similarly, the bitwise inverse of any positive integer will be the negative of the next larger number.
Steven Parker
231,271 PointsAnother peculiarity is that these operators are being applied to a boolean value resulting from a comparison. But when you coerce it into an integer, the two's complement rules still apply. So when no_referrals_index
is True, The negative will be -1 and the bitwise inverse will be -2. When it is False, the negative will be 0 and the bitwise inverse will be -1.