a BitSet class

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

a BitSet class

Torsten Bergmann
Hi Pablo,

can you be more specific why you need a BitSet class other than convinience? What do you
want to do?

Note that for bit handling in Smalltalk you can easily just send messages to integers
or large integers:

Access bits:    12 bitAt: 1              -> 0
Modify bits:    12 bitAt: 1 put: 1       -> 13
Print binary:   12 printStringBase: 2    -> '1100'
Print hex:      12 printStringBase: 16    -> 'C'
Large number:   67677557657 printStringBase: 2  ->  '111111000001111001011001001110011001'

There are also #bitAnd:, #bitOr:, #bitXor: messages:

   12 bitOr: 1  -> 13
   12 bitAnd: 8 -> 8

and you can use the abbreviations known from C world:

   12 | 1    -> 13
   12 & 1    -> 0

If you want to see the bit representation just print as a string with base 2:
 
  12 | 2 printStringBase: 2  ->  '1110'

So for octal use:    

  12 | 2 printStringBase: 8  -> '16'

If you disklike to stay in decimal system you should be aware that in
Smalltalk you can easily write a number in different bases directly
using the "BASE, followed by r followed by number" representation.

So you can for example write:

    12
as  2r1100
as 16rC  
as 10r12 (decimal again)
as 8r14 (octal)
or any other base that you like directly

With this you can also easily manipulate bits while writing in bit representation:

          2r0001 bitOr: 2r0101  -> 5
same as:  2r0001 | 2r0101       -> 5
 
when you need the result again as bits:

 (2r0001 | 2r0101) printStringBase: 2  ->  '101'

Many other convinience methods are there like:
  12 hex                      -> '16rC'
  12 printStringHex           -> 'C'
  $A hex                      -> '16r41'
  $A printStringHex           -> '41'

Feel free to mix all that

  (16rFF00 | 2r00001) hex  ->  '16rFF01'

Try to get used to these methods and you will have fun with bits
in Pharo Smalltalk easily.

If you need something more tell us, a BitSet class implementation
could be easily written using the above as a base.

Bye
T.
 







Reply | Threaded
Open this post in threaded view
|

Re: a BitSet class

pharo4Stef@free.fr
torsten

could you check that all your examples are really covered in the deepintopharo chapter about little numbers?
Else we should add them.

Stef
On 06 Feb 2014, at 16:08, Torsten Bergmann <[hidden email]> wrote:

> Hi Pablo,
>
> can you be more specific why you need a BitSet class other than convinience? What do you
> want to do?
>
> Note that for bit handling in Smalltalk you can easily just send messages to integers
> or large integers:
>
> Access bits:    12 bitAt: 1              -> 0
> Modify bits:    12 bitAt: 1 put: 1       -> 13
> Print binary:   12 printStringBase: 2    -> '1100'
> Print hex:      12 printStringBase: 16    -> 'C'
> Large number:   67677557657 printStringBase: 2  ->  '111111000001111001011001001110011001'
>
> There are also #bitAnd:, #bitOr:, #bitXor: messages:
>
>   12 bitOr: 1  -> 13
>   12 bitAnd: 8 -> 8
>
> and you can use the abbreviations known from C world:
>
>   12 | 1    -> 13
>   12 & 1    -> 0
>
> If you want to see the bit representation just print as a string with base 2:
>
>  12 | 2 printStringBase: 2  ->  '1110'
>
> So for octal use:    
>
>  12 | 2 printStringBase: 8  -> '16'
>
> If you disklike to stay in decimal system you should be aware that in
> Smalltalk you can easily write a number in different bases directly
> using the "BASE, followed by r followed by number" representation.
>
> So you can for example write:
>
>    12
> as  2r1100
> as 16rC  
> as 10r12 (decimal again)
> as 8r14 (octal)
> or any other base that you like directly
>
> With this you can also easily manipulate bits while writing in bit representation:
>
>          2r0001 bitOr: 2r0101  -> 5
> same as:  2r0001 | 2r0101       -> 5
>
> when you need the result again as bits:
>
> (2r0001 | 2r0101) printStringBase: 2  ->  '101'
>
> Many other convinience methods are there like:
>  12 hex                      -> '16rC'
>  12 printStringHex           -> 'C'
>  $A hex                      -> '16r41'
>  $A printStringHex           -> '41'
>
> Feel free to mix all that
>
>  (16rFF00 | 2r00001) hex  ->  '16rFF01'
>
> Try to get used to these methods and you will have fun with bits
> in Pharo Smalltalk easily.
>
> If you need something more tell us, a BitSet class implementation
> could be easily written using the above as a base.
>
> Bye
> T.
>
>
>
>
>
>
>
>