PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 19, 2022

[FIXED] when I write SELECT ~1; on Postgresql it gives me -2 as a result. What is the reason of this? And it keeps going for ~4 and -5 etc

 July 19, 2022     integer, postgresql, tilde     No comments   

Issue

I was looking for sql basics and I decided to write SELECT ~1; on postgresql and as a result it returned -2. When i tried this for bigger number it always returned me integer's next integer and negative (~300 and -301). I wonder what's the reason.


Solution

~ evidently is the bitwise negation operator. 1 = 0b00001, ~1 = 0b111...110 = -2.

This is also called the ones' complement. As you know the numbers use two's complement as representation for negative numbers (as then -0 == 0):

-x == ~x+1
~x == -x-1


Answered By - Joop Eggen
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing