unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* doco on bitwise logicals
@ 2003-05-04  0:56 Kevin Ryde
  2003-05-04  5:35 ` Rob Browning
  2003-05-05 23:31 ` Kevin Ryde
  0 siblings, 2 replies; 10+ messages in thread
From: Kevin Ryde @ 2003-05-04  0:56 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 2336 bytes --]

I thought it might be nice to add some overall words about twos
complement negatives for the bitwise functions, and I think ash can be
clarified by talking about that.  Without wanting to be critical, I
don't think "not guarantee to keep the bit-structure of N" is as
enlightening as it could be.

	* scheme-data.texi (Bitwise Operations): Note negatives are treated as
	infinite precision twos complement.  Revise `ash' to emphasise this
	for right shifts of negatives.  Describe integer-length behaviour on
	negatives.  Add `...' to logand, logior, logxor since they take
	multiple parameters.

Assuming all this is meant to be documented features of course :-).

It occurs to me that integer-length is perhaps not ideal in returning
0 for an argument of -1.  But very likely it's too late to change
that.

New text for ease of contemplation:


For the following bitwise functions, negative numbers are treated as
infinite precision twos-complements.  For instance -6 is bits
...111010, with infinitely many ones on the left.  It can be seen that
adding 6 (binary 110) to such a bit pattern gives all zeros.


 - Scheme Procedure: ash n cnt
 - C Function: scm_ash (n, cnt)
     Return N shifted left by CNT bits, or shifted right if CNT is
     negative.  This is an "arithmetic" shift.

     This corresponds to a multiplication by 2^CNT.  When CNT is
     negative it's a division, and the rounding is towards negative
     infinity.  (Note that this is not the same rounding as `quotient'
     does.)

     With N viewed as an infinite precision twos complement, `ash'
     means a left shift introducing zero bits, or a right shift
     dropping bits.  For instance -23 is ...11101001, which shifted by
     a CNT of -2 drops two bits to give ...111010, which is -6.

          (number->string (ash #b1 3) 2)     => "1000"
          (number->string (ash #b1010 -1) 2) => "101"

 - Scheme Procedure: integer-length n
 - C Function: scm_integer_length (n)
     Return the number of bits necessary to represent N.

     For positive N this is how many bits to the highest one bit.  For
     negative N it's how many bits to the highest zero bit in twos
     complement form.

          (integer-length #b10101010)
             => 8
          (integer-length 0)
             => 0
          (integer-length #b1111)
             => 4



[-- Attachment #2: scheme-data.texi.bitwise.diff --]
[-- Type: text/plain, Size: 3218 bytes --]

--- scheme-data.texi.~1.26.~	2003-05-04 08:43:27.000000000 +1000
+++ scheme-data.texi	2003-05-04 10:49:27.000000000 +1000
@@ -1055,7 +1055,13 @@
 @node Bitwise Operations
 @subsection Bitwise Operations
 
-@deffn {Scheme Procedure} logand n1 n2
+For the following bitwise functions, negative numbers are treated as
+infinite precision twos-complements.  For instance @math{-6} is bits
+@math{@dots{}111010}, with infinitely many ones on the left.  It can
+be seen that adding 6 (binary 110) to such a bit pattern gives all
+zeros.
+
+@deffn {Scheme Procedure} logand n1 n2 @dots{}
 Return the bitwise @sc{and} of the integer arguments.
 
 @lisp
@@ -1065,7 +1071,7 @@
 @end lisp
 @end deffn
 
-@deffn {Scheme Procedure} logior n1 n2
+@deffn {Scheme Procedure} logior n1 n2 @dots{}
 Return the bitwise @sc{or} of the integer arguments.
 
 @lisp
@@ -1075,9 +1081,10 @@
 @end lisp
 @end deffn
 
-@deffn {Scheme Procedure} logxor n1 n2
+@deffn {Scheme Procedure} logxor n1 n2 @dots{}
 Return the bitwise @sc{xor} of the integer arguments.  A bit is
 set in the result if it is set in an odd number of arguments.
+
 @lisp
 (logxor) @result{} 0
 (logxor 7) @result{} 7
@@ -1088,8 +1095,8 @@
 
 @deffn {Scheme Procedure} lognot n
 @deffnx {C Function} scm_lognot (n)
-Return the integer which is the 2s-complement of the integer
-argument.
+Return the integer which is the ones-complement of the integer
+argument, ie.@: each 0 bit is changed to 1 and each 1 bit to 0.
 
 @lisp
 (number->string (lognot #b10000000) 2)
@@ -1124,16 +1131,19 @@
 
 @deffn {Scheme Procedure} ash n cnt
 @deffnx {C Function} scm_ash (n, cnt)
-The function @code{ash} performs an arithmetic shift left by @var{cnt}
-bits (or shift right, if @var{cnt} is negative).  `Arithmetic'
-means that the function does not guarantee to keep the bit
-structure of @var{n}, but rather guarantees that the result
-will always be rounded towards minus infinity.  Therefore, the
-results of @code{ash} and a corresponding bitwise shift will differ if
-@var{n} is negative.
+Return @var{n} shifted left by @var{cnt} bits, or shifted right if
+@var{cnt} is negative.  This is an ``arithmetic'' shift.
 
-Formally, the function returns an integer equivalent to
-@code{(inexact->exact (floor (* @var{n} (expt 2 @var{cnt}))))}.
+This corresponds to a multiplication by @math{2^@var{cnt}}.  When
+@var{cnt} is negative it's a division, and the rounding is towards
+negative infinity.  (Note that this is not the same rounding as
+@code{quotient} does.)
+
+With @var{n} viewed as an infinite precision twos complement,
+@code{ash} means a left shift introducing zero bits, or a right shift
+dropping bits.  For instance @math{-23} is @math{@dots{}11101001},
+which shifted by a @var{cnt} of @math{-2} drops two bits to give
+@math{@dots{}111010}, which is @math{-6}.
 
 @lisp
 (number->string (ash #b1 3) 2)     @result{} "1000"
@@ -1162,6 +1172,10 @@
 @deffnx {C Function} scm_integer_length (n)
 Return the number of bits necessary to represent @var{n}.
 
+For positive @var{n} this is how many bits to the highest one bit.
+For negative @var{n} it's how many bits to the highest zero bit in
+twos complement form.
+
 @lisp
 (integer-length #b10101010)
    @result{} 8

[-- Attachment #3: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel

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

* Re: doco on bitwise logicals
  2003-05-04  0:56 doco on bitwise logicals Kevin Ryde
@ 2003-05-04  5:35 ` Rob Browning
  2003-05-05 23:35   ` Kevin Ryde
  2003-05-05 23:31 ` Kevin Ryde
  1 sibling, 1 reply; 10+ messages in thread
From: Rob Browning @ 2003-05-04  5:35 UTC (permalink / raw)
  Cc: guile-devel

Kevin Ryde <user42@zip.com.au> writes:

> It occurs to me that integer-length is perhaps not ideal in returning
> 0 for an argument of -1.

I was just trying to figure that (and negative integer-length) out
myself...

> New text for ease of contemplation:

Looks quite good to me.

Two things that I thought of were wondering if we could think of
something other than "highest FOO bit", that might be clearer.  (Would
"most significant FOO bit" be appropriate or more confusing?), and
wondering if you might want to include some negative n examples in the
integer-length verbage and example set.

Thanks

>
>
> For the following bitwise functions, negative numbers are treated as
> infinite precision twos-complements.  For instance -6 is bits
> ...111010, with infinitely many ones on the left.  It can be seen that
> adding 6 (binary 110) to such a bit pattern gives all zeros.
>
>
>  - Scheme Procedure: ash n cnt
>  - C Function: scm_ash (n, cnt)
>      Return N shifted left by CNT bits, or shifted right if CNT is
>      negative.  This is an "arithmetic" shift.
>
>      This corresponds to a multiplication by 2^CNT.  When CNT is
>      negative it's a division, and the rounding is towards negative
>      infinity.  (Note that this is not the same rounding as `quotient'
>      does.)
>
>      With N viewed as an infinite precision twos complement, `ash'
>      means a left shift introducing zero bits, or a right shift
>      dropping bits.  For instance -23 is ...11101001, which shifted by
>      a CNT of -2 drops two bits to give ...111010, which is -6.
>
>           (number->string (ash #b1 3) 2)     => "1000"
>           (number->string (ash #b1010 -1) 2) => "101"
>
>  - Scheme Procedure: integer-length n
>  - C Function: scm_integer_length (n)
>      Return the number of bits necessary to represent N.
>
>      For positive N this is how many bits to the highest one bit.  For
>      negative N it's how many bits to the highest zero bit in twos
>      complement form.
>
>           (integer-length #b10101010)
>              => 8
>           (integer-length 0)
>              => 0
>           (integer-length #b1111)
>              => 4
>
>
>
> _______________________________________________
> Guile-devel mailing list
> Guile-devel@gnu.org
> http://mail.gnu.org/mailman/listinfo/guile-devel

-- 
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592  F9A0 25C8 D377 8C7E 73A4


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


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

* Re: doco on bitwise logicals
  2003-05-04  0:56 doco on bitwise logicals Kevin Ryde
  2003-05-04  5:35 ` Rob Browning
@ 2003-05-05 23:31 ` Kevin Ryde
  2003-05-06  1:23   ` Rob Browning
  1 sibling, 1 reply; 10+ messages in thread
From: Kevin Ryde @ 2003-05-05 23:31 UTC (permalink / raw)


I wrote:
>
> 	* scheme-data.texi (Bitwise Operations): ...

Also sneaking in here (in the patch but not stated) is a fix for
lognot to say ones-complement, where it says twos-complement now.
This would be a fix for the 1.6 branch as well as the mainline I
think.

 - Scheme Procedure: lognot n
 - C Function: scm_lognot (n)
     Return the integer which is the ones-complement of the integer
     argument, ie. each 0 bit is changed to 1 and each 1 bit to 0.

          (number->string (lognot #b10000000) 2)
             => "-10000001"
          (number->string (lognot #b0) 2)
             => "-1"


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


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

* Re: doco on bitwise logicals
  2003-05-04  5:35 ` Rob Browning
@ 2003-05-05 23:35   ` Kevin Ryde
  2003-05-10  1:10     ` Kevin Ryde
  0 siblings, 1 reply; 10+ messages in thread
From: Kevin Ryde @ 2003-05-05 23:35 UTC (permalink / raw)


Rob Browning <rlb@defaultvalue.org> writes:
>
> Two things that I thought of were wondering if we could think of
> something other than "highest FOO bit", that might be clearer.  (Would
> "most significant FOO bit" be appropriate or more confusing?)

Yes, most significant would be good.

> and wondering if you might want to include some negative n examples
> in the integer-length verbage and example set.

Yep, new text:

 - Scheme Procedure: integer-length n
 - C Function: scm_integer_length (n)
     Return the number of bits necessary to represent N.

     For positive N this is how many bits to the most significant one
     bit.  For negative N it's how many bits to the most significant
     zero bit in twos complement form.

          (integer-length #b10101010) => 8
          (integer-length #b1111)     => 4
          (integer-length 0)          => 0
          (integer-length -1)         => 0
          (integer-length -256)       => 8
          (integer-length -257)       => 9


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


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

* Re: doco on bitwise logicals
  2003-05-05 23:31 ` Kevin Ryde
@ 2003-05-06  1:23   ` Rob Browning
  2003-05-06  2:02     ` Kevin Ryde
  2003-05-08  1:07     ` Kevin Ryde
  0 siblings, 2 replies; 10+ messages in thread
From: Rob Browning @ 2003-05-06  1:23 UTC (permalink / raw)
  Cc: guile-devel

Kevin Ryde <user42@zip.com.au> writes:

> Also sneaking in here (in the patch but not stated) is a fix for
> lognot to say ones-complement, where it says twos-complement now.
> This would be a fix for the 1.6 branch as well as the mainline I
> think.

Yes.  Any fixes that are really documentation or bug fixes that you
have the time to commit to 1.6, please do.

Also, if you haven't dealt with two branches yet, I've found it
helpful to just keep two trees checked out, one on 1.6 via "cvs update
-r branch_release_1-6 -Pd", and the other on the unstable branch
(i.e. HEAD)...

(here's hoping we can move to subversion one of these days...)

Thanks very much for the work.

-- 
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592  F9A0 25C8 D377 8C7E 73A4


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


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

* Re: doco on bitwise logicals
  2003-05-06  1:23   ` Rob Browning
@ 2003-05-06  2:02     ` Kevin Ryde
  2003-05-08  1:07     ` Kevin Ryde
  1 sibling, 0 replies; 10+ messages in thread
From: Kevin Ryde @ 2003-05-06  2:02 UTC (permalink / raw)


Rob Browning <rlb@defaultvalue.org> writes:
>
> Yes.  Any fixes that are really documentation or bug fixes that you
> have the time to commit to 1.6, please do.

I thought in the branch I'd just look at outright errors, but anyone
who thinks more should go in can say so.


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


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

* Re: doco on bitwise logicals
  2003-05-06  1:23   ` Rob Browning
  2003-05-06  2:02     ` Kevin Ryde
@ 2003-05-08  1:07     ` Kevin Ryde
  2003-05-09 23:12       ` Rob Browning
  1 sibling, 1 reply; 10+ messages in thread
From: Kevin Ryde @ 2003-05-08  1:07 UTC (permalink / raw)


Rob Browning <rlb@defaultvalue.org> writes:
>
> -r branch_release_1-6

I made the change, hopefully its hit the right place ...


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


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

* Re: doco on bitwise logicals
  2003-05-08  1:07     ` Kevin Ryde
@ 2003-05-09 23:12       ` Rob Browning
  0 siblings, 0 replies; 10+ messages in thread
From: Rob Browning @ 2003-05-09 23:12 UTC (permalink / raw)
  Cc: guile-devel

Kevin Ryde <user42@zip.com.au> writes:

> Rob Browning <rlb@defaultvalue.org> writes:
>>
>> -r branch_release_1-6
>
> I made the change, hopefully its hit the right place ...

As long as your tree was updated to that branch, then any commits
should go on the branch.  In case you don't already know, you can tell
if you're on the branch by looking at a file's status to see if
there's a sticky tag:

  cvs status ChangeLog | grep 'Sticky Tag:'
     Sticky Tag:          branch_release-1-6 (branch: 1.281.2)

-- 
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592  F9A0 25C8 D377 8C7E 73A4


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


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

* Re: doco on bitwise logicals
  2003-05-05 23:35   ` Kevin Ryde
@ 2003-05-10  1:10     ` Kevin Ryde
  2003-05-10  3:58       ` Kevin Ryde
  0 siblings, 1 reply; 10+ messages in thread
From: Kevin Ryde @ 2003-05-10  1:10 UTC (permalink / raw)


I made this change, but with some tweaks to what I posted for ash:

 - Scheme Procedure: ash n cnt
 - C Function: scm_ash (n, cnt)
     Return N shifted left by CNT bits, or shifted right if CNT is
     negative.  This is an "arithmetic" shift.

     This is effectively a multiplication by 2^CNT, and when CNT is
     negative it's a division, rounded towards negative infinity.
     (Note that this is not the same rounding as `quotient' does.)

     With N viewed as an infinite precision twos complement, `ash'
     means a left shift introducing zero bits, or a right shift
     dropping bits.

          (number->string (ash #b1 3) 2)     => "1000"
          (number->string (ash #b1010 -1) 2) => "101"

          ;; -23 is bits ...11101001, -6 is bits ...111010
          (ash -23 -2) => -6


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


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

* Re: doco on bitwise logicals
  2003-05-10  1:10     ` Kevin Ryde
@ 2003-05-10  3:58       ` Kevin Ryde
  0 siblings, 0 replies; 10+ messages in thread
From: Kevin Ryde @ 2003-05-10  3:58 UTC (permalink / raw)


I wrote:
>
> 2^CNT

Almost forgot, I introduced

        * guile.texi (m): New macro.

to allow different expressions for tex and non-tex maths.  It's good
for things like "x*y" for multiply in info, but just "xy" for tex.

In this case though it avoids problems with @var{} superscripts
(there's no font for that on my system, not sure if there's supposed
to be but I've struck the same on freebsd too).


_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


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

end of thread, other threads:[~2003-05-10  3:58 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-04  0:56 doco on bitwise logicals Kevin Ryde
2003-05-04  5:35 ` Rob Browning
2003-05-05 23:35   ` Kevin Ryde
2003-05-10  1:10     ` Kevin Ryde
2003-05-10  3:58       ` Kevin Ryde
2003-05-05 23:31 ` Kevin Ryde
2003-05-06  1:23   ` Rob Browning
2003-05-06  2:02     ` Kevin Ryde
2003-05-08  1:07     ` Kevin Ryde
2003-05-09 23:12       ` Rob Browning

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).