The Trunk: Kernel-nice.402.mcz

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

The Trunk: Kernel-nice.402.mcz

commits-2
Nicolas Cellier uploaded a new version of Kernel to project The Trunk:
http://source.squeak.org/trunk/Kernel-nice.402.mcz

==================== Summary ====================

Name: Kernel-nice.402
Author: nice
Time: 16 February 2010, 10:08:26.271 am
UUID: 804739fb-f633-4d4d-9717-2b69cea9c2cb
Ancestors: Kernel-ar.401

Naive implementation of Integer>>bitAt:put:

=============== Diff against Kernel-ar.401 ===============

Item was added:
+ ----- Method: Integer>>bitAt:put: (in category 'bit manipulation') -----
+ bitAt: anInteger put: value
+ "Answer a new Integer that has the bit of rank anInteger set to value.
+ The bit value should be 0 or 1, otherwise raise an Error.
+ The bits are indexed starting at 1 for the least significant bit.
+ For negative integers, operate on 2-complement representation."
+
+ | b |
+ b := self bitAt: anInteger.
+ b = value ifTrue: [^self].
+ 0 = value ifTrue: [^self bitAnd: (1 bitShift: anInteger - 1) bitInvert].
+ 1 = value ifTrue: [^self bitOr: (1 bitShift: anInteger - 1)].
+ self error: 'bit value should be 0 or 1'!


Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-nice.402.mcz

Michael Haupt-3
Hi,

On Tue, Feb 16, 2010 at 10:08 AM,  <[hidden email]> wrote:
> +       "Answer a new Integer that has the bit of rank anInteger set to value.
> +       The bit value should be 0 or 1, otherwise raise an Error.
> +       The bits are indexed starting at 1 for the least significant bit.
> +       For negative integers, operate on 2-complement representation."

hm. I understand that Smalltalk's idiomatic way of using 1-based
indexing is applied here, but the indices into bits of integers
correlate fairly strong with the powers of two they represent. I'd
argue that using 0 for the LSB is more appropriate with regard to the
domain being modelled here.

Also, I'd suggest to raise the error early on, instead of at the end
of the entire method.

Best,

Michael

Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-nice.402.mcz

Bert Freudenberg
On 16.02.2010, at 10:16, Michael Haupt wrote:

>
> Hi,
>
> On Tue, Feb 16, 2010 at 10:08 AM,  <[hidden email]> wrote:
>> +       "Answer a new Integer that has the bit of rank anInteger set to value.
>> +       The bit value should be 0 or 1, otherwise raise an Error.
>> +       The bits are indexed starting at 1 for the least significant bit.
>> +       For negative integers, operate on 2-complement representation."
>
> hm. I understand that Smalltalk's idiomatic way of using 1-based
> indexing is applied here, but the indices into bits of integers
> correlate fairly strong with the powers of two they represent. I'd
> argue that using 0 for the LSB is more appropriate with regard to the
> domain being modelled here.

Jerome mentioned this is an ANSI method, so we should follow the standard. What does it say?

- Bert -



Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-nice.402.mcz

Michael Haupt-3
Hi,

On Tue, Feb 16, 2010 at 10:28 AM, Bert Freudenberg <[hidden email]> wrote:
> Jerome mentioned this is an ANSI method, so we should follow the standard. What does it say?

it says "The least significant bit of the receiver is designated as
bit 1, with indices increasing to the left.", and I think that's a bad
example of domain implementation. Yuck. But who am I to question a
STANDARD? ;-)

Best,

Michael

Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-nice.402.mcz

Nicolas Cellier
In reply to this post by Michael Haupt-3
2010/2/16 Michael Haupt <[hidden email]>:

> Hi,
>
> On Tue, Feb 16, 2010 at 10:08 AM,  <[hidden email]> wrote:
>> +       "Answer a new Integer that has the bit of rank anInteger set to value.
>> +       The bit value should be 0 or 1, otherwise raise an Error.
>> +       The bits are indexed starting at 1 for the least significant bit.
>> +       For negative integers, operate on 2-complement representation."
>
> hm. I understand that Smalltalk's idiomatic way of using 1-based
> indexing is applied here, but the indices into bits of integers
> correlate fairly strong with the powers of two they represent. I'd
> argue that using 0 for the LSB is more appropriate with regard to the
> domain being modelled here.
>
> Also, I'd suggest to raise the error early on, instead of at the end
> of the entire method.
>
> Best,
>
> Michael
>
>

Hi Michael,

I understand your argument, but 1-based is the ANSI standard...

I did not want to test pre-conditions, because most time, users will
provide a 0 or 1 value and thus we will just waste time testing for
nothing.
But this could make sense to use precondition with another implementation:

    (#(0 1) includes: value) ifFalse: [self error: '...'].
    ^(self bitOr: (1 bitShift: anInteger - 1)) - (value bitShift: anInteger - 1)

Nicolas

Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-nice.402.mcz

Nicolas Cellier
In reply to this post by Michael Haupt-3
2010/2/16 Michael Haupt <[hidden email]>:

> Hi,
>
> On Tue, Feb 16, 2010 at 10:28 AM, Bert Freudenberg <[hidden email]> wrote:
>> Jerome mentioned this is an ANSI method, so we should follow the standard. What does it say?
>
> it says "The least significant bit of the receiver is designated as
> bit 1, with indices increasing to the left.", and I think that's a bad
> example of domain implementation. Yuck. But who am I to question a
> STANDARD? ;-)
>
> Best,
>

Before I read your mail, I was precisely thinking of this extension for fun:

(0 bitAt: -1 put: 1) = (1/2).
(0 bitAt: -2 put: 1) = (1/4).

but sure, 1 based is spoiling this game...

> Michael
>
>

Reply | Threaded
Open this post in threaded view
|

Re: The Trunk: Kernel-nice.402.mcz

Nicolas Cellier
In reply to this post by Nicolas Cellier
2010/2/16 Nicolas Cellier <[hidden email]>:

> 2010/2/16 Michael Haupt <[hidden email]>:
>> Hi,
>>
>> On Tue, Feb 16, 2010 at 10:08 AM,  <[hidden email]> wrote:
>>> +       "Answer a new Integer that has the bit of rank anInteger set to value.
>>> +       The bit value should be 0 or 1, otherwise raise an Error.
>>> +       The bits are indexed starting at 1 for the least significant bit.
>>> +       For negative integers, operate on 2-complement representation."
>>
>> hm. I understand that Smalltalk's idiomatic way of using 1-based
>> indexing is applied here, but the indices into bits of integers
>> correlate fairly strong with the powers of two they represent. I'd
>> argue that using 0 for the LSB is more appropriate with regard to the
>> domain being modelled here.
>>
>> Also, I'd suggest to raise the error early on, instead of at the end
>> of the entire method.
>>
>> Best,
>>
>> Michael
>>
>>
>
> Hi Michael,
>
> I understand your argument, but 1-based is the ANSI standard...
>
> I did not want to test pre-conditions, because most time, users will
> provide a 0 or 1 value and thus we will just waste time testing for
> nothing.
> But this could make sense to use precondition with another implementation:
>
>    (#(0 1) includes: value) ifFalse: [self error: '...'].
>    ^(self bitOr: (1 bitShift: anInteger - 1)) - (value bitShift: anInteger - 1)
>

Stupid me, it's more like
^(self bitOr: (1 bitShift: anInteger - 1)) - ((1-value) bitShift: anInteger - 1)

> Nicolas
>