Skip site navigation (1) Skip section navigation (2)

Re: How do i extract a certain bit from a bigint column

From: Mike Rylander <miker(at)purplefrog(dot)com>
To: <pgsql-sql(at)postgresql(dot)org>
Cc: Mats Sjöberg <mats(dot)sjoberg(at)cybernetics(dot)se>
Subject: Re: How do i extract a certain bit from a bigint column
Date: 2004-05-12 12:17:39
Message-ID: 200405120817.39593.miker@purplefrog.com (view raw or flat)
Thread:
Lists: pgsql-sql
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

In response to

Responses

pgsql-sql by date

Next:From: Tom LaneDate: 2004-05-12 12:55:47
Subject: Re: How do i extract a certain bit from a bigint column
Previous:From: Mats SjöbergDate: 2004-05-12 11:05:09
Subject: How do i extract a certain bit from a bigint column

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group