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 8088899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:13:38+00:00 2026-06-05T19:13:38+00:00

In Java, I would like to store (>10’000) arrays of boolean values (boolean[]) with

  • 0

In Java, I would like to store (>10’000) arrays of boolean values (boolean[]) with length 32 to the disk and read them again later on for further computation and comparison.

Since a single array will have a length of 32, I wonder whether it makes sense to store it as an integer value to speed up the reading and writing (on a 32 bit machine). Would you suggest using BitSet and then convert to int? Or even forget about int and use bytes?

  • 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-06-05T19:13:41+00:00Added an answer on June 5, 2026 at 7:13 pm

    For binary storage, use int and a DataOutputStream (DataInputStream for reading).

    I think boolean arrays are stored as byte or int arrays internally in Java, so you may want to consider avoiding the overhead and keeping the int encoding all the time, i.e. not use boolean[] at all.

    Instead, have something like

    public class BooleanArray32 {
      private int values;
    
      public boolean get(int pos) {
        return (values & (1 << pos)) != 0;
      }
    
      public void set(int pos, boolean value) {
         int mask = 1 << pos;
         values = (values & ~mask) | (value ? mask : 0);
      }
    
      public void write(DataOutputStream dos) throws IOException {
        dos.writeInt(values);
      }
    
      public void read(DataInputStream dis) throws IOException {
        values = dis.readInt();
      }
    
      public int compare(BooleanArray32 b2) {
         return countBits(b2.values & values);
      }
    
      // From http://graphics.stanford.edu/~seander/bithacks.html
      // Disclaimer: I did not fully double check whether this works for Java's signed ints
      public static int countBits(int v) {
        v = v - ((v >>> 1) & 0x55555555);                    // reuse input as temporary
        v = (v & 0x33333333) + ((v >>> 2) & 0x33333333);     // temp
        return ((v + (v >>> 4) & 0xF0F0F0F) * 0x1010101) >>> 24; 
      }
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to store the properties I read from a Java type property
We would like to some processing in a Java application, store the results in
In java i would like to read a file line by line and print
I would like to store multiple values for a single key and must be
I would like to store information in a java collection that is cached in
I have a single user java program that I would like to have store
I would like to store tuples objects in a concurent java collection and then
I would like to read an entire text file and store its entire contents
I have made a small application in Java and I would like to make
I would like to create a java List based on another java Collection eg.

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.