Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 12BBB1337B49 for ; Mon, 9 May 2011 03:42:09 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 82248-03 for ; Mon, 9 May 2011 06:42:01 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from www.postnewspapers.com.au (www.postnewspapers.com.au [173.230.139.50]) by mail.postgresql.org (Postfix) with ESMTP id 20500133616B for ; Mon, 9 May 2011 03:42:00 -0300 (ADT) Received: from localhost (localhost [127.0.0.1]) by www.postnewspapers.com.au (Postfix) with ESMTP id 64E2437; Mon, 9 May 2011 02:42:00 -0400 (EDT) X-Virus-Scanned: amavisd-new at postnewspapers.com.au.com Received: from www.postnewspapers.com.au ([127.0.0.1]) by localhost (www.postnewspapers.com.au [127.0.0.1]) (amavisd-new, port 10024) with LMTP id OE8+zAyC5p2P; Mon, 9 May 2011 02:41:59 -0400 (EDT) Received: from mail.postnewspapers.com.au (ppp171-128.static.internode.on.net [150.101.171.128]) by www.postnewspapers.com.au (Postfix) with ESMTP id 7EDC227; Mon, 9 May 2011 02:41:59 -0400 (EDT) Received: from [10.0.0.53] (wallace.localnet [10.0.0.53]) by mail.postnewspapers.com.au (Postfix) with ESMTP id 78929AE46F; Mon, 9 May 2011 14:41:58 +0800 (WST) Message-ID: <4DC78CB6.9080101@postnewspapers.com.au> Date: Mon, 09 May 2011 14:41:58 +0800 From: Craig Ringer User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.15) Gecko/20110307 Thunderbird/3.1.9 MIME-Version: 1.0 To: Marco Spiga CC: pgsql-bugs@postgresql.org Subject: Re: BUG #6015: to_hex and negative integer References: <201105081807.p48I7fgA087261@wwwmaster.postgresql.org> In-Reply-To: <201105081807.p48I7fgA087261@wwwmaster.postgresql.org> X-Enigmail-Version: 1.1.2 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201105/48 X-Sequence-Number: 30187 On 09/05/11 02:07, Marco Spiga wrote: > > The following bug has been logged online: > > Bug reference: 6015 > Logged by: Marco Spiga > Email address: ctxspi@gmail.com > PostgreSQL version: 8.3.12 > Operating system: Ubuntu Linux on kernel version 2.6.30.5 > Description: to_hex and negative integer > Details: > > Hi! > First, thank for your excellent software. > My question is: > Why when I try: SELECT to_hex(-1) > it give me a strange result: ffffffff That's not a bug, it's exactly what I'd expect to happen. What did you expect to get instead? -0x01 ? 0xffff? 0xffffffffffffffff? Don't understand why the result is the expected one by most people? Read this: http://en.wikipedia.org/wiki/Two's_complement ... so you understand how signed numbers are represented internally by most binary computer systems. As it happens, the real problem with to_hex() on a negative number is that the result doesn't make much sense unless you know the size of the representation. For example, ffff can mean 65535 if interpreted as an unsigned 16-bit interger, or can mean -1 if interpreted as a signed 16-bit int. ffffffff can mean 4294967295 if interpreted as an unsigned 32 bit int or as a signed 64-bit integer, but is -1 if interpreted as a signed 32-bit integer. Essentially, to_hex takes the _size_ of the input datum as an implicit input. This means that you should NOT USE IT in any context where you do not know the data type of the input, because its results vary in meaning depending on what you feed into it. classads=> select to_hex(-1 :: integer); to_hex ---------- ffffffff (1 row) classads=> select to_hex(-1 :: bigint); to_hex ------------------ ffffffffffffffff (1 row) -- Craig Ringer