PostgreSQL:bit to smallint

RGP*_*GPT 3 sql postgresql bit-manipulation 16-bit

据我所知,在PostgreSQL中你无法从十六进制或位转换为smallint或其他方式.

要从int2转换为bit16,可以执行以下操作:

select ((((-32768)::int2)::int4)::bit(32)<<16)::bit(16)
    Result: 1000000000000000
Run Code Online (Sandbox Code Playgroud)

但是我们怎么能这样做呢?

我有一个int2标志,我想设置最高位.但是因为我不能在int2中使用我的位操作,所以我必须先将它转换为int4,所以我可以这样做.像这样的东西:

SELECT flags, 
       (flags | x'8000'::integer) myInt2Result 
  FROM MyTable;
Run Code Online (Sandbox Code Playgroud)

然后我会使用myInt2Result来调用其他进程.为了让它更容易尝试,让我们想象标志是一个值为2560的smallint:

SELECT 2560::int2, (2560::int2 | x'8000'::integer)
    RESULT: 35328        
Run Code Online (Sandbox Code Playgroud)

由于这大于+32767并且我在PostgreSQL中没有unsigned smallint,我无法将其直接转换为int2(smallint超出范围).

另外:在PostgreSQL中我们做不到

x'8000'::int2 (it would be really handy) 
OR
x'8000'::integer::int2 (smallint out of range) 
Run Code Online (Sandbox Code Playgroud)

有没有办法在PostgreSQL中执行此操作,或者我必须自己将int4转换为int2(考虑位)?

pdw*_*pdw 5

以下表达式适用于PostgreSQL 9.1:

select ((-32768)::int2)::int4::bit(16);
==>  X'8000'

select ((('X8000'::bit(16))::bit(32)::int4) >> 16)::int2;
==> -32768
Run Code Online (Sandbox Code Playgroud)

编辑:一些证据表明这是有效的:

-- int2 to bit16 and back
create temp table test1 (x int2);
insert into test1 select generate_series(-32768,32767)::int2;
select x from test1 where x != ((x::int4::bit(16) ::bit(32)::int4) >> 16)::int2;
==> no rows selected

-- bit16 to int2 and back
create temp table test2 (x bit(16));
insert into test2 select generate_series(0,65536)::bit(16);
select x from test2 where x != (((x::bit(32)::int4)>>16)::int2) ::int4::bit(16);
==> no rows selected
Run Code Online (Sandbox Code Playgroud)