开发者社区> 问答> 正文

关于postgresql运算符计算问题

已解决

我们在开发过程中需要在数据库中编写函数,完成CRC校验计算,但是发现了一个问题,请帮忙解决一下!


java程序


public static int UpdateCRC(int crc, final int c) {
    return ((crc >> 8) ^ c) & 0xFF;
}

crc=0 c=-86
返回值为170


postgresql


declare
crc int;
c int;
BEGIN
crc:= 0;
c:=-86;
DBMS_OUTPUT.PUT_LINE(((crc >> 8)^c) & (255::double));
end;
执行报错,请您看看哪里出了问题?

展开
收起
穿山甲201603 2016-03-04 18:16:29 4464 0
3 条回答
写回答
取消 提交回答
  • 公益是一辈子的事, I am digoal, just do it. 阿里云数据库团队, 擅长PolarDB, PostgreSQL, DuckDB, ADB等, 长期致力于推动开源数据库技术、生态在中国的发展与开源产业人才培养. 曾荣获阿里巴巴麒麟布道师称号、2018届OSCAR开源尖峰人物.
    采纳回答

    你的例子中用到了>>,^,&操作符,这些操作符对应的操作数类型如下:

    postgres=# \do+ >>
                                               List of operators
       Schema   | Name | Left arg type | Right arg type | Result type |   Function    |     Description     
    ------------+------+---------------+----------------+-------------+---------------+---------------------
     pg_catalog | >>   | anyrange      | anyrange       | boolean     | range_after   | is right of
     pg_catalog | >>   | bigint        | integer        | bigint      | int8shr       | bitwise shift right
     pg_catalog | >>   | bit           | integer        | bit         | bitshiftright | bitwise shift right
     pg_catalog | >>   | box           | box            | boolean     | box_right     | is right of
     pg_catalog | >>   | circle        | circle         | boolean     | circle_right  | is right of
     pg_catalog | >>   | inet          | inet           | boolean     | network_sup   | is supernet
     pg_catalog | >>   | integer       | integer        | integer     | int4shr       | bitwise shift right
     pg_catalog | >>   | point         | point          | boolean     | point_right   | is right of
     pg_catalog | >>   | polygon       | polygon        | boolean     | poly_right    | is right of
     pg_catalog | >>   | smallint      | integer        | smallint    | int2shr       | bitwise shift right
    (10 rows)
    
    postgres=# \do+ &
                                          List of operators
       Schema   | Name | Left arg type | Right arg type | Result type |  Function   | Description 
    ------------+------+---------------+----------------+-------------+-------------+-------------
     pg_catalog | &    | bigint        | bigint         | bigint      | int8and     | bitwise and
     pg_catalog | &    | bit           | bit            | bit         | bitand      | bitwise and
     pg_catalog | &    | inet          | inet           | inet        | inetand     | bitwise and
     pg_catalog | &    | integer       | integer        | integer     | int4and     | bitwise and
     pg_catalog | &    | macaddr       | macaddr        | macaddr     | macaddr_and | bitwise and
     pg_catalog | &    | smallint      | smallint       | smallint    | int2and     | bitwise and
    (6 rows)
    
    postgres=# \do+ ^
                                                  List of operators
       Schema   | Name |  Left arg type   |  Right arg type  |   Result type    |   Function    |  Description   
    ------------+------+------------------+------------------+------------------+---------------+----------------
     pg_catalog | ^    | double precision | double precision | double precision | dpow          | exponentiation
     pg_catalog | ^    | numeric          | numeric          | numeric          | numeric_power | exponentiation
    (2 rows)
    
    postgres=# select ((2)^(-86)) & 0xFF;
    ERROR:  operator does not exist: double precision & integer
    LINE 1: select ((2)^(-86)) & 0xFF;
                               ^
    HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.

    报错的是&这个操作符,因为^产生了numeric的结果,所以需要转成&支持的类型才行。
    解决办法,转换成int8:

    postgres=# select ((2)^(-86))::int8 & 0xFF;
     xff 
    -----
       0
    (1 row)

    如果要实现JAVA程序一样的效果,需要修改一下写法,^在PG里是pow,不是亦或。亦或是#符号。

    postgres=# select ((0) # (-86))::int4 & 255;
     ?column? 
    ----------
          170
    (1 row)
    2019-07-17 18:32:36
    赞同 展开评论 打赏
  • 今天在遇到同类问题: date类型数据 减去 (默认精度)numeric数据类型。一直提示:Error:operator does not exists :date - numeric。
    显示转换 ::integer,解决。 多谢。

    2019-07-17 18:32:36
    赞同 展开评论 打赏
  • 阿里云PostgreSQL和Greenplum内核开发
    位运算不能是double类型,必须是整形,所以不能把255强制转换成double

    -------------------------

    把错误日志也贴出来

    2019-07-17 18:32:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
云栖大会:开源 PolarDB 架构演进、关键技术与社区建设 立即下载
2023云栖大会:和客户一起玩转PolarDB新特性 立即下载
2023云栖大会:PolarDB for AI 立即下载