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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:32:06+00:00 2026-05-27T04:32:06+00:00

I’m just beginning to learn about file compression and I’ve run into a bit

  • 0

I’m just beginning to learn about file compression and I’ve run into a bit of a roadblock. I have an application that will encode a string such as “program” as a compressed binary representation "010100111111011000"(note this is still stored as a String).

Encoding
g       111
r       10
a       110
p       010
o       011
m       00

Now I need to write this to the file system using a FileOutputStream, the problem I’m having is, how can I convert the string “010100111111011000” to a byte[]/bytes to be written to the file system with FileOutputStream?

I’ve never worked with bits/bytes before so I’m kind of at a dead end here.

  • 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-27T04:32:07+00:00Added an answer on May 27, 2026 at 4:32 am

    An introduction to bit-shift operators:

    First, we have the left-shift operator, x << n. This will shift all the bits in x left by n bits, filling the new bits with zero:

          1111 1111 
    << 3: 1111 1000
    

    Next, we have the signed right-shift operator, x >> n. This shifts all the bits in x right by n, copying the sign bit into the new bits:

          1111 1111 
    >> 3: 1111 1111
    
          1000 0000
    >> 3: 1111 0000
    
          0111 1111 
    >> 3: 0000 1111
    

    Finally, we have the zero-fill right-shift operator, x >>> n. This shifts all bits in x right by n bits, filling the new bits with zero:

           1111 1111 
    >>> 3: 0001 1111
    

    You may also find useful the bitwise-or operator, x | y. This compares the bits in each position in x and y, setting the new number’s bit on if it was on in either x or y, off otherwise:

      1010 0101
    | 1010 1010
      ---------
      1010 1111
    

    You should only need the previous operators for the problem at hand, but for the sake of completeness, here are the last two:

    The bitwise-and operator, x & y sets the bits in the output to one if and only if the bit is on in both x and y:

      1010 0101
    & 1010 1010
      ---------
      1010 0000
    

    The bitwise-xor operator, x ^ y sets the output bits to one if the bit is on in one number or the other but not both:

      1010 0101
    ^ 1010 1010
      ---------
      0000 1111
    

    Now, applying these to the situation at hand:

    You will need to use the bit-shift operators to add and manipulate bits. Start setting bits at the right side according to their string representations and shift them over. Continue until you hit the end of a byte, and then move to the next byte. Say we want to create a byte representation of “1100 1010”:

    Our byte    Target
    ---------   --------
    0000 0000
                1100 1010
    0000 0001   ^
                1100 1010
    0000 0011    ^
                1100 1010
    0000 0110     ^
                1100 1010
    0000 1100      ^
                1100 1010
    0001 1001        ^
                1100 1010
    0011 0010         ^
                1100 1010
    0110 0101          ^
                1100 1010
    1100 1010           ^
    

    I will, of course, leave it to you to apply this to your work.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a reasonable size flat file database of text documents mostly saved in
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't

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.