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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:06:54+00:00 2026-05-18T03:06:54+00:00

To read/write binary files, I am using DataInputStream/DataOutputStream, they have this method writeByte()/readByte(), but

  • 0

To read/write binary files, I am using DataInputStream/DataOutputStream, they have this method writeByte()/readByte(), but what I want to do is read/write bits? Is it possible?

I want to use it for a compression algorithm, so when I am compressing I want to write 3 bits(for one number and there are millions of such numbers in a file) and if I write a byte at everytime I need to write 3 bits, I will write loads of redundant data…

  • 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-18T03:06:55+00:00Added an answer on May 18, 2026 at 3:06 am

    It’s not possible to read/write individual bits directly, the smallest unit you can read/write is a byte.

    You can use the standard bitwise operators to manipulate a byte though, so e.g. to get the lowest 2 bits of a byte, you’d do

    byte b = in.readByte();
    byte lowBits = b&0x3;
    

    set the low 4 bits to 1, and write the byte:

    b |= 0xf;
    out.writeByte(b);
    

    (Note, for the sake of efficiency you might want to read/write byte arrays and not single bytes)

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

Sidebar

Related Questions

I am using the cstdio (stdio.h) to read and write data from binary files.
I'm currently using fopen to write/read binary files. With small files all is fines.
I have a file that I want to read and write to a binary
I've read the post Read and write from/to a binary file in Matlab but
How to write binary data into XML using LINQ which is being read from
Note: I'm aware about the disadvantages of using BinaryFormatter in large files. But this
I have code that manipulates binary files using fstream with the binary flag set
i want to read\write a binary file which has the following structure: The file
I have this PHP function (using PHP 5.3) that I use to decrypt files,
I have to concatenate 3 binary files into one, but I am getting an

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.