BUG #4114: Inconsistent shift operator

Started by Roman Kononovalmost 18 years ago3 messagesbugs
Jump to latest
#1Roman Kononov
kononov@dls.net

The following bug has been logged online:

Bug reference: 4114
Logged by: Roman Kononov
Email address: kononov@dls.net
PostgreSQL version: 8.3.1
Operating system: x86_64 GNU/Linux
Description: Inconsistent shift operator
Details:

The below test cases show the obvious inconsistency between different
integer types.

test=# \t
Showing only tuples.
test=# select 1::int2 << 17;
0

test=# select 1::int4 << 33;
2

test=# select 1::int8 << 65;
2

test=# select 2::int2 >> 17;
0

test=# select 2::int4 >> 33;
1

test=# select 2::int8 >> 65;
1

#2Zdenek Kotala
Zdenek.Kotala@Sun.COM
In reply to: Roman Kononov (#1)
Re: BUG #4114: Inconsistent shift operator

Roman Kononov napsal(a):

The following bug has been logged online:

Bug reference: 4114
Logged by: Roman Kononov
Email address: kononov@dls.net
PostgreSQL version: 8.3.1
Operating system: x86_64 GNU/Linux
Description: Inconsistent shift operator
Details:

The below test cases show the obvious inconsistency between different
integer types.

test=# \t
Showing only tuples.
test=# select 1::int2 << 17;
0

test=# select 1::int4 << 33;
2

test=# select 1::int8 << 65;
2

test=# select 2::int2 >> 17;
0

test=# select 2::int4 >> 33;
1

test=# select 2::int8 >> 65;
1

It seems to be OK regarding how C shift operator works. Try

#include <stdio.h>
#include <inttypes.h>

void fce(int16_t arg1, int32_t arg2)
{
int16_t res = arg1 << arg2;
printf("result: %i\n", res);
}

int main()
{
fce(1,17);
return 0;
}

Zdenek

#3Sam Mason
sam@samason.me.uk
In reply to: Zdenek Kotala (#2)
Re: BUG #4114: Inconsistent shift operator

On Sun, Apr 20, 2008 at 08:17:50PM +0200, Zdenek Kotala wrote:

Roman Kononov napsal(a):

The below test cases show the obvious inconsistency between different
integer types.

It seems to be OK regarding how C shift operator works. Try

Yes, but I interpret this behaviour as not being very useful for people
writing SQL code that uses the shift operators. C is a very low level
language and you almost always end up writing platform specific code,
SQL is supposed to be much higher level and should abstract away system
specific differences.

I've not been able to think of a nice way of doing this though.

Sam