pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Mike Rylander <miker@purplefrog.com>
To: pgsql-sql@postgresql.org
Cc: Mats Sjöberg <mats.sjoberg@cybernetics.se>
Subject: Re: How do i extract a certain bit from a bigint column
Date: Wed, 12 May 2004 08:17:39 -0400
Message-ID: <200405120817.39593.miker@purplefrog.com> (raw)
In-Reply-To: <A40C30932F52C4409B89ED049CCA06B13BA41D@srvex01.hk.cybernetics.se>
References: <A40C30932F52C4409B89ED049CCA06B13BA41D@srvex01.hk.cybernetics.se>

On Wednesday 12 May 2004 07:05 am, Mats Sjöberg wrote:
> Hello everyone
> In a table i have a column status of type bigint.
> I need to create a view of the table including all rows with bit 4 set
> (value 8).
> At the same time i need to exclude excludig all rows with bit 2 set.
>
> What is the syntax to extract those bits?
> I have tested get_bit(string, offset) but this requires a string, and not a
> bigint.
> Is there a function to

The easiest way is to test for a bit using bitwise and:

SELECT * FROM table WHERE (status & 2::BIGINT) <> 0 AND (status & 8::BIGINT) = 
0;

-miker



view thread (4+ messages)  latest in thread

Message-ID: <200405120817.39593.miker@purplefrog.com>
Permalink:  ../200405120817.39593.miker@purplefrog.com/
Also on:    postgresql.org/message-id/200405120817.39593.miker@purplefrog.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: miker@purplefrog.com, mats.sjoberg@cybernetics.se
  Subject: Re: How do i extract a certain bit from a bigint column
  In-Reply-To: <200405120817.39593.miker@purplefrog.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox