Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 541387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:20:17+00:00 2026-05-13T10:20:17+00:00

As an exercise I am working on a parallel implementation of the Sieve of

  • 0

As an exercise I am working on a parallel implementation of the Sieve of Eratosthenes. As part of that I am implementing a sequence of bitmaps, using one bit per number to save memory. Reading bits one at a time appear to work fine, but setting them is slow, especially when I use large binaries.

getBit(Bin, N, Size)->
    R=Size-N-1,
    <<_:N,Bit:1,_:R>> = Bin,
    Bit.

setBit(Bin, N, Size)->
    R=Size-N-1,
    <<A:N,_:1,B:R>> = Bin,
    <<A:N,1:1,B:R>>.

Is there a way to do this well in functional Erlang, perhaps similar to how Arrays work?
I have read about hipe_bifs:bytearray_update but would prefer to keep my coding style functional.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T10:20:18+00:00Added an answer on May 13, 2026 at 10:20 am

    You can store your bitmaps as integers (if they are not longer than ~1000 bits):

    -module(z).
    -export([get_bit/2, set_bit/2]).
    
    get_bit(N, B) -> N band bit_to_int(B) > 0.
    set_bit(N, B) -> N bor bit_to_int(B).
    
    bit_to_int(B) -> 1 bsl B.
    

    You might try your version w/o passing the Size, and padding to bytes:

    getBit(Bin, N)->
        <<_:N/bitstring,Bit:1,_/bitstring>> = Bin,
        Bit.
    
    setBit(Bin, N)->
        <<A:N,_:1,B/bitstring>> = Bin,
        <<A:N,1:1,B>>.
    
    %OR:
    setBit(Bin, N)->
        PSize = 8 - ((N + 1) rem 8),
        <<A:N,_:1, Pad:PSize,B/bytes>> = Bin,
        <<A:N,1:1,Pad:PSize,B>>.
    

    Can be better or worse 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on the exercise questions of book The Lambda calculus . One
While working through Real World Haskell, I tried to complete the palindrome exercise using
I am working on a programming exercise that has interested me for some time.
i'm working through SICP - one exercise is to implement foreach (doseq). This is
I am working on a POSIX C learning exercise that involves recursively listing files/folders
As part of learning WPF I just finished working through an MS Lab Exercise
I'm working on an exercise that wants me to create a small twitter clone,
I'm working on a small webapp that generates exercise program printouts. A user (ie
As an exercise for myself, I'm implementing the Miller-Rabin test. (Working through SICP). I
I've been working through Practical Common Lisp and as an exercise decided to write

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.