How to create a bit array / bit vector

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How to create a bit array / bit vector

maverik a
Hi All,

I'm trying to create a bit array in Erlang with simple new() , set() and get() operations. 

My new() looks like:
new(Max) ->
N = 1 bsl Max-1,
<<N>>.

but problem I'm stuck with is, my max can be at most 8 .

How can I create a bitstring of Max length ?

Regards
Maverick


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to create a bit array / bit vector

Robert Carbone
Hey Maverick -


Perhaps what you are looking for is this?

fill(Len) -> <<(bnot 0):Len>>.


Cheers,

Robert Carbone

scriptculture.com


On 2/26/19 5:09 PM, maverik a wrote:
Hi All,

I'm trying to create a bit array in Erlang with simple new() , set() and get() operations. 

My new() looks like:
new(Max) ->
N = 1 bsl Max-1,
<<N>>.

but problem I'm stuck with is, my max can be at most 8 .

How can I create a bitstring of Max length ?

Regards
Maverick


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to create a bit array / bit vector

dieter
In reply to this post by maverik a
Hi,

how about

new(Max) ->
  <<1:1, 0:(Max-1)>>.

This creates a binary with Max bits, with the MSB set to 1.

I found this doc page very helpful:

Kind regards,
Dieter



Am Mi., Febr. 27, 2019 06:13 schrieb maverik a <[hidden email]>:
Hi All,

I'm trying to create a bit array in Erlang with simple new() , set() and get() operations. 

My new() looks like:
new(Max) ->
N = 1 bsl Max-1,
<<N>>.

but problem I'm stuck with is, my max can be at most 8 .

How can I create a bitstring of Max length ?

Regards
Maverick


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: How to create a bit array / bit vector

maverik a
Hi Dieter,

Thank you that did work.
And thanks for sharing the doc, the constructing binaries part explains this.

Regards
Maverick

On Wed, Feb 27, 2019 at 8:41 AM <[hidden email]> wrote:
Hi,

how about

new(Max) ->
  <<1:1, 0:(Max-1)>>.

This creates a binary with Max bits, with the MSB set to 1.

I found this doc page very helpful:

Kind regards,
Dieter



Am Mi., Febr. 27, 2019 06:13 schrieb maverik a <[hidden email]>:
Hi All,

I'm trying to create a bit array in Erlang with simple new() , set() and get() operations. 

My new() looks like:
new(Max) ->
N = 1 bsl Max-1,
<<N>>.

but problem I'm stuck with is, my max can be at most 8 .

How can I create a bitstring of Max length ?

Regards
Maverick


_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions