1

In SSMS, "SELECT ~ 0" gives -1 as the result. Similar results with other numbers too. I would like to know why.

Thanks!

Strayda
  • 27
  • 6
  • 1
    https://learn.microsoft.com/en-us/sql/t-sql/language-elements/bitwise-not-transact-sql?view=sql-server-ver15 – GSerg Apr 20 '21 at 13:26
  • Does this answer your question? [What is “2's Complement”?](https://stackoverflow.com/questions/1049722/what-is-2s-complement) – Charlieface Apr 20 '21 at 19:16

1 Answers1

5

~ is Bitwise NOT

0 is 0x00000000, ~0 is 0xFFFFFFFF and since int is signed, and negative numbers are stored using Two's complement

select cast(0xFFFFFFFF as int)

outputs

-1

David Browne - Microsoft
  • 80,331
  • 6
  • 39
  • 67