pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Addition and subtraction on BIT type
7+ messages / 3 participants
[nested] [flat]

* Addition and subtraction on BIT type
@ 2003-11-16 16:17  Yasir Malik <ymalik@heineken.cs.stevens-tech.edu>
  0 siblings, 0 replies; 7+ messages in thread

From: Yasir Malik @ 2003-11-16 16:17 UTC (permalink / raw)
  To: pgsql-sql

Hello,
Is there a way to do addition and subtraction on BIT types?  For example,
for
creat table test (a BIT(3));
insert into test values (B'101');

select a + 1 from test; fails

and select a::smallint + 1 from test; also fails.

In addition, is there a way to change the bit of a bit string?  For
example change a 1 to a 0 or vice versa.

Any suggestions?
Thanks,
Yasir



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Addition and subtraction on BIT type
@ 2003-11-16 16:18  Yasir Malik <ymalik@cs.stevens-tech.edu>
  0 siblings, 1 reply; 7+ messages in thread

From: Yasir Malik @ 2003-11-16 16:18 UTC (permalink / raw)
  To: pgsql-sql

Hello,
Is there a way to do addition and subtraction on BIT types?  For example,
for
creat table test (a BIT(3));
insert into test values (B'101');

select a + 1 from test; fails

and select a::smallint + 1 from test; also fails.

In addition, is there a way to change the bit of a bit string?  For
example change a 1 to a 0 or vice versa.

Any suggestions?
Thanks,
Yasir



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Addition and subtraction on BIT type
@ 2003-11-17 02:02  Yasir Malik <ymalik@cs.stevens-tech.edu>
  parent: Yasir Malik <ymalik@cs.stevens-tech.edu>
  0 siblings, 1 reply; 7+ messages in thread

From: Yasir Malik @ 2003-11-17 02:02 UTC (permalink / raw)
  To: pgsql-sql

Hello,
I think I am almost at a solution to my last question.  I can do
select int4(a) from test;
to convert to an integer.  So now addition and
subtraction can be done between bit types.  But how do I convert back to
BIT type?  If I do
select bit(int4(b'1001'));

I get the following message:
ERROR:  parser: parse error at or near "int4" at character 12

Can anyone tell me why the bit function is not working?  It's under the
pg_catalog schema.
Thanks,
Yasir

On Sun, 16 Nov 2003, Yasir Malik wrote:

> Date: Sun, 16 Nov 2003 11:18:03 -0500
> From: Yasir Malik <ymalik@cs.stevens-tech.edu>
> To: pgsql-sql@postgresql.org
> Subject: Addition and subtraction on BIT type
>
> Hello,
> Is there a way to do addition and subtraction on BIT types?  For example,
> for
> creat table test (a BIT(3));
> insert into test values (B'101');
>
> select a + 1 from test; fails
>
> and select a::smallint + 1 from test; also fails.
>
> In addition, is there a way to change the bit of a bit string?  For
> example change a 1 to a 0 or vice versa.
>
> Any suggestions?
> Thanks,
> Yasir
>



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Addition and subtraction on BIT type
@ 2003-11-17 05:40  Stephan Szabo <sszabo@megazone.bigpanda.com>
  parent: Yasir Malik <ymalik@cs.stevens-tech.edu>
  0 siblings, 1 reply; 7+ messages in thread

From: Stephan Szabo @ 2003-11-17 05:40 UTC (permalink / raw)
  To: Yasir Malik <ymalik@cs.stevens-tech.edu>; +Cc: pgsql-sql

On Sun, 16 Nov 2003, Yasir Malik wrote:

> I think I am almost at a solution to my last question.  I can do
> select int4(a) from test;
> to convert to an integer.  So now addition and
> subtraction can be done between bit types.  But how do I convert back to
> BIT type?  If I do
> select bit(int4(b'1001'));
>
> I get the following message:
> ERROR:  parser: parse error at or near "int4" at character 12
>
> Can anyone tell me why the bit function is not working?  It's under the
> pg_catalog schema.

It's also the name of a type that takes a precision in parentheses, so
you'd have to say "bit"(...) with the quotes. As a note, I think
that's going to effectively return you a bit(32), so

sszabo=# select "bit"(int4(b'1001'));
               bit
----------------------------------
 00000000000000000000000000001001




^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Addition and subtraction on BIT type
@ 2003-11-17 14:10  Yasir Malik <ymalik@heineken.cs.stevens-tech.edu>
  parent: Stephan Szabo <sszabo@megazone.bigpanda.com>
  0 siblings, 1 reply; 7+ messages in thread

From: Yasir Malik @ 2003-11-17 14:10 UTC (permalink / raw)
  To: Stephan Szabo <sszabo@megazone.bigpanda.com>; +Cc: pgsql-sql

Thank you for your reply.
select int4(b'1001')::bit(32); gives the same result as what you gave.
select int4(b'1001')::bit(4); gives the upper four bits, which are all
zeroes.  How would I get the lower four bits?  I building bitmaps using
plpgsql, and therefore, I will be doing a lot bit manipulation.
Thanks,
Yasir

On Sun, 16 Nov 2003, Stephan Szabo wrote:

> Date: Sun, 16 Nov 2003 21:40:45 -0800 (PST)
> From: Stephan Szabo <sszabo@megazone.bigpanda.com>
> To: Yasir Malik <ymalik@cs.stevens-tech.edu>
> Cc: pgsql-sql@postgresql.org
> Subject: Re: [SQL] Addition and subtraction on BIT type
>
> On Sun, 16 Nov 2003, Yasir Malik wrote:
>
> > I think I am almost at a solution to my last question.  I can do
> > select int4(a) from test;
> > to convert to an integer.  So now addition and
> > subtraction can be done between bit types.  But how do I convert back to
> > BIT type?  If I do
> > select bit(int4(b'1001'));
> >
> > I get the following message:
> > ERROR:  parser: parse error at or near "int4" at character 12
> >
> > Can anyone tell me why the bit function is not working?  It's under the
> > pg_catalog schema.
>
> It's also the name of a type that takes a precision in parentheses, so
> you'd have to say "bit"(...) with the quotes. As a note, I think
> that's going to effectively return you a bit(32), so
>
> sszabo=# select "bit"(int4(b'1001'));
>                bit
> ----------------------------------
>  00000000000000000000000000001001
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Addition and subtraction on BIT type
@ 2003-11-17 14:32  Yasir Malik <ymalik@heineken.cs.stevens-tech.edu>
  0 siblings, 0 replies; 7+ messages in thread

From: Yasir Malik @ 2003-11-17 14:32 UTC (permalink / raw)
  To: pgsql-sql; +Cc: sszabo@megazone.bigpanda.com

Thank you for your reply.
select int4(b'1001')::bit(32); gives the same result as what you gave.
select int4(b'1001')::bit(4); gives the upper four bits, which are all
zeroes.  How would I get the lower four bits?  I building bitmaps using
plpgsql, and therefore, I will be doing a lot bit manipulation.
Thanks,
Yasir

On Sun, 16 Nov 2003, Stephan Szabo wrote:

> Date: Sun, 16 Nov 2003 21:40:45 -0800 (PST)
> From: Stephan Szabo <sszabo@megazone.bigpanda.com>
> To: Yasir Malik <ymalik@cs.stevens-tech.edu>
> Cc: pgsql-sql@postgresql.org
> Subject: Re: [SQL] Addition and subtraction on BIT type
>
> On Sun, 16 Nov 2003, Yasir Malik wrote:
>
> > I think I am almost at a solution to my last question.  I can do
> > select int4(a) from test;
> > to convert to an integer.  So now addition and
> > subtraction can be done between bit types.  But how do I convert back to
> > BIT type?  If I do
> > select bit(int4(b'1001'));
> >
> > I get the following message:
> > ERROR:  parser: parse error at or near "int4" at character 12
> >
> > Can anyone tell me why the bit function is not working?  It's under the
> > pg_catalog schema.
>
> It's also the name of a type that takes a precision in parentheses, so
> you'd have to say "bit"(...) with the quotes. As a note, I think
> that's going to effectively return you a bit(32), so
>
> sszabo=# select "bit"(int4(b'1001'));
>                bit
> ----------------------------------
>  00000000000000000000000000001001
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org
>



^ permalink  raw  reply  [nested|flat] 7+ messages in thread

* Re: Addition and subtraction on BIT type
@ 2003-11-17 15:18  Stephan Szabo <sszabo@megazone.bigpanda.com>
  parent: Yasir Malik <ymalik@heineken.cs.stevens-tech.edu>
  0 siblings, 0 replies; 7+ messages in thread

From: Stephan Szabo @ 2003-11-17 15:18 UTC (permalink / raw)
  To: Yasir Malik <ymalik@heineken.cs.stevens-tech.edu>; +Cc: pgsql-sql

On Mon, 17 Nov 2003, Yasir Malik wrote:

> Thank you for your reply.
> select int4(b'1001')::bit(32); gives the same result as what you gave.
> select int4(b'1001')::bit(4); gives the upper four bits, which are all
> zeroes.  How would I get the lower four bits?  I building bitmaps using
> plpgsql, and therefore, I will be doing a lot bit manipulation.

I think you need to use substring, probably something like:
select substring(9::bit(32) from 29 for 4);



^ permalink  raw  reply  [nested|flat] 7+ messages in thread


end of thread, other threads:[~2003-11-17 15:18 UTC | newest]

Thread overview: 7+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-11-16 16:17 Addition and subtraction on BIT type Yasir Malik <ymalik@heineken.cs.stevens-tech.edu>
2003-11-16 16:18 Addition and subtraction on BIT type Yasir Malik <ymalik@cs.stevens-tech.edu>
2003-11-17 02:02 ` Yasir Malik <ymalik@cs.stevens-tech.edu>
2003-11-17 05:40   ` Stephan Szabo <sszabo@megazone.bigpanda.com>
2003-11-17 14:10     ` Yasir Malik <ymalik@heineken.cs.stevens-tech.edu>
2003-11-17 15:18       ` Stephan Szabo <sszabo@megazone.bigpanda.com>
2003-11-17 14:32 Re: Addition and subtraction on BIT type Yasir Malik <ymalik@heineken.cs.stevens-tech.edu>

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