dbTalk Databases Forums  

results of shift on integers

comp.databases.postgresql comp.databases.postgresql


Discuss results of shift on integers in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Coniglio Sgabbiato
 
Posts: n/a

Default results of shift on integers - 09-22-2009 , 05:07 AM






Hi all, is it the behaviour shown below normal?
I mean, why 1::int << 32 and 1::bigint << 64 gives 1 instead to return
an error?

db_atm=# select (1::bigint << 64)-1 as test;
test
------
0
(1 row)

db_atm=# select (1::bigint << 63)-1 as test;
ERROR: bigint out of range

db_atm=# select (1::bigint << 62)-1 as test;
test
---------------------
4611686018427387903
(1 row)

db_atm=# select (1::int << 32)-1 as test;
test
------
0
(1 row)

db_atm=# select (1::int << 31)-1 as test;
ERROR: integer out of range

db_atm=# select (1::int << 30)-1 as test;
test
------------
1073741823
(1 row)

Reply With Quote
  #2  
Old   
Coniglio Sgabbiato
 
Posts: n/a

Default Re: results of shift on integers - 09-22-2009 , 05:09 AM






I understood now...

db_atm=# select (1::bigint << 64);
?column?
----------
1
(1 row)

db_atm=# select (1::bigint << 65);
?column?
----------
2
(1 row)

db_atm=# select (1::bigint << 66);
?column?
----------
4
(1 row)


Coniglio Sgabbiato ha scritto:
Quote:
Hi all, is it the behaviour shown below normal?
I mean, why 1::int << 32 and 1::bigint << 64 gives 1 instead to return
an error?

db_atm=# select (1::bigint << 64)-1 as test;
test
------
0
(1 row)

db_atm=# select (1::bigint << 63)-1 as test;
ERROR: bigint out of range

db_atm=# select (1::bigint << 62)-1 as test;
test
---------------------
4611686018427387903
(1 row)

db_atm=# select (1::int << 32)-1 as test;
test
------
0
(1 row)

db_atm=# select (1::int << 31)-1 as test;
ERROR: integer out of range

db_atm=# select (1::int << 30)-1 as test;
test
------------
1073741823
(1 row)

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.