Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Bitwise not (~)

Can someone please explain why

select ~1

gives the result

-2

Perhaps there is a lot for me to learn about the actual bits of number types. What resource would you recommend?

like image 771
Seattle Leonard Avatar asked Dec 17 '25 19:12

Seattle Leonard


1 Answers

Q: Can someone please explain why select ~1 gives the result -2?

A: For the same reason ~0 would give -1 :)

Here's a good article on "twos complement" arithmetic:

  • http://en.wikipedia.org/wiki/Twos_complement

Most CPU architectures are two's complement (vs. one's complement). From the same article:

Two's complement is the easiest to implement in hardware, which may be the ultimate reason for its widespread popularity[citation needed]. Processors on the early mainframes often consisted of thousands of transistors – eliminating a significant number of transistors was a significant cost savings. The architects of the early integrated circuit-based CPUs (Intel 8080, etc.) chose to use two's complement math. As IC technology advanced, virtually all adopted two's complement technology. Intel, AMD, and Power Architecture chips are all two's complement.

like image 174
paulsm4 Avatar answered Dec 20 '25 13:12

paulsm4



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!