PL/pgSQL function returns incorrect bitwise result

Martin Perry

I have a PL/pgSQL function callbackfunc. If I run it with bitwise result and unpack the result in my app, I got 65/0/65/65 (which is 1090535745). If I return directly result of bitwise operation (1094795585) and unpack it, I got 65/65/65/65. Why?

See sample below

CREATE OR REPLACE FUNCTION callbackfunc(value double precision[][][], pos integer[][], VARIADIC userargs text[])
    RETURNS double precision
    AS $$
DECLARE    
     var_result double precision;   
BEGIN
        var_result := (65 << 24 | 65 << 16 | 65 << 8 | 65 << 0);
        --var_result := 1094795585;   
    RETURN var_result;
END;
$$ LANGUAGE 'plpgsql' IMMUTABLE;
clemens

It's the operator's precedence. Bitwise-Or is higher than Shift. Your expression will be calculated as

var_result := (((((((65 << 24) | 65) << 16) | 65) << 8) | 65) << 0);

which gives 1090535745 (= 0x41004141). On the opposite

var_result := (65 << 24) | (65 << 16) | (65 << 8) | (65 << 0)

gives 1094795585 (= 0x41414141).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related