unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* logical shift operators in guile?
@ 2010-06-09  4:04 steve tell
  2010-06-09 22:45 ` Andy Wingo
  2010-06-10 12:32 ` Thien-Thi Nguyen
  0 siblings, 2 replies; 4+ messages in thread
From: steve tell @ 2010-06-09  4:04 UTC (permalink / raw)
  To: Guile Mailing List



Does guile (1.8.x, or any version) have the integer logical shift 
operators, like C's << and >> ?


While searching for those, I realized that what I was trying to do was 
represent small sets of booleans as the bits of an integer... and that 
somthing similar could be done in a more scheme-like fashion with 
bitvectors.

But is there any way to copy the contents of bitvector a to another 
bitvector b of the same size, short of iterating over the elements?


thanks,
Steve






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

* Re: logical shift operators in guile?
  2010-06-09  4:04 logical shift operators in guile? steve tell
@ 2010-06-09 22:45 ` Andy Wingo
  2010-06-10  3:38   ` dsmich
  2010-06-10 12:32 ` Thien-Thi Nguyen
  1 sibling, 1 reply; 4+ messages in thread
From: Andy Wingo @ 2010-06-09 22:45 UTC (permalink / raw)
  To: steve tell; +Cc: Guile Mailing List

Hi Steve,

On Wed 09 Jun 2010 06:04, steve tell <tell@telltronics.org> writes:

> Does guile (1.8.x, or any version) have the integer logical shift
> operators, like C's << and >> ?

We have ash (arithmetic shift) but not lsh (logical shift). I admit I am
somewhat ignorant regarding when you would prefer lsh over ash. If it is
important (as it probably is) we should add lsh.

> While searching for those, I realized that what I was trying to do was
> represent small sets of booleans as the bits of an integer... and that
> somthing similar could be done in a more scheme-like fashion with
> bitvectors.
>
> But is there any way to copy the contents of bitvector a to another
> bitvector b of the same size, short of iterating over the elements?

There does not seem to be, though perhaps I am overlooking something.
There is array-copy!, but that is not implemented in the most efficient
way for contiguous uniform vectors like bitvectors. Would you like to
submit a patch to add bitvector-copy and/or bitvector-copy! ?

For my eye there's something still not right as far as bitvectors'
implementation. I guess I would prefer something with a bytevector as a
backing store... But that's a topic for another day.

Regards,

Andy
-- 
http://wingolog.org/



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

* Re: logical shift operators in guile?
  2010-06-09 22:45 ` Andy Wingo
@ 2010-06-10  3:38   ` dsmich
  0 siblings, 0 replies; 4+ messages in thread
From: dsmich @ 2010-06-10  3:38 UTC (permalink / raw)
  To: steve tell, Andy Wingo; +Cc: Guile Mailing List


---- Andy Wingo <wingo@pobox.com> wrote: 
> Hi Steve,
> 
> On Wed 09 Jun 2010 06:04, steve tell <tell@telltronics.org> writes:
> 
> > Does guile (1.8.x, or any version) have the integer logical shift
> > operators, like C's << and >> ?
> 
> We have ash (arithmetic shift) but not lsh (logical shift). I admit I am
> somewhat ignorant regarding when you would prefer lsh over ash. If it is
> important (as it probably is) we should add lsh.

It depends on what you do with the sign bit when shifting to the right on 2's complement.

Logical shift fills with 0's and arithmetic shift fills with the sign bit.

The "logical" functions in guile do work on bignums.  No need mess with anything else.

-Dale




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

* Re: logical shift operators in guile?
  2010-06-09  4:04 logical shift operators in guile? steve tell
  2010-06-09 22:45 ` Andy Wingo
@ 2010-06-10 12:32 ` Thien-Thi Nguyen
  1 sibling, 0 replies; 4+ messages in thread
From: Thien-Thi Nguyen @ 2010-06-10 12:32 UTC (permalink / raw)
  To: steve tell; +Cc: Guile Mailing List

() steve tell <tell@telltronics.org>
() Wed, 9 Jun 2010 00:04:41 -0400 (EDT)

   But is there any way to copy the contents of bitvector a to another
   bitvector b of the same size, short of iterating over the elements?

You can use ‘bit-set*!’, something like:

(define (copy-bit-vector orig)
  (let ((copy (make-uniform-vector (uniform-vector-length orig) #t)))
    (bit-invert! copy)
    (bit-set*! copy orig #t)
    copy))

(write-line (copy-bit-vector #*111111111111111))
#*111111111111111

This uses Guile 1.4.x-isms for constructing the bit vector; YMMV.
Here is a more elegant variant that does not work under Guile 1.4.1.118,
even though it should (a bug):

(define (copy-bit-vector orig)
  (let ((copy (make-uniform-vector (uniform-vector-length orig) #t)))
    (bit-set*! copy orig #f)
    copy))

The first variant creates all-1s, "manually" inverts, then does a logical OR.
The second creates all-1s, then does a logical AND-NOT.

BTW, to answer the previous question (re shift), you can avoid shifting by
specifying a non-bitvec uniform vector as the second arg to ‘bit-set*!’.

thi



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

end of thread, other threads:[~2010-06-10 12:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-06-09  4:04 logical shift operators in guile? steve tell
2010-06-09 22:45 ` Andy Wingo
2010-06-10  3:38   ` dsmich
2010-06-10 12:32 ` Thien-Thi Nguyen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).