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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:18:27+00:00 2026-06-17T12:18:27+00:00

I have two related problems (in Perl): Write data to binary files, in the

  • 0

I have two related problems (in Perl):

  1. Write data to binary files, in the format: single bit flag followed by 8 bits
  2. Read back the same format

I tried this (and other variations but for the life of me I can’t figure this out):

binmode(OUT);
my $bit = pack("B1", '1');
my $byte = pack("H2", "02");
print OUT $bit . $byte;

Using a hex editor, I see I get 16 bits:

1000000000000020

What I want is 9 bits:

100000020

Also: Suppose I write out two of these patterns. That means I end up with 9 + 9 = 18 bits. I am not sure how to handle the last byte (padding?)

This is to compress and uncompress files, with space at premium. I was hoping there would be some simple idiomatic way to do this that I am not aware of.

  • 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-17T12:18:28+00:00Added an answer on June 17, 2026 at 12:18 pm

    You can use Bit::Vector to manage your bits and conversion with some more ease,

    use Bit::Vector;
    
    my $bit = Bit::Vector->new_Bin( 1, '1' );
    
    my $byte     = Bit::Vector->new_Bin( 8, '00000010' );
    my $byte_9   = Bit::Vector->new_Bin( 9, '000000010' );
    my $nineBits = Bit::Vector->new_Bin( 9, '100000000' );
    my $carry    = Bit::Vector->new_Bin( 9, '000000000' );
    my $ORed     = Bit::Vector->new_Bin( 9, '000000000' );
    my $added    = Bit::Vector->new_Bin( 9, '000000000' );
    
    
    $ORed->Union($nineBits,$byte_9);
    
    print "bit: 0x". $bit->to_Hex(). "\n";
    print "byte 2: 0x". $byte->to_Hex(). "\n";
    print "nineBits: 0x". $nineBits->to_Hex(). "\n";
    print "nineBits: 0x". $nineBits->to_Bin(). "\n";
    print "ORed bit and byte 0x". $ORed->to_Dec(). "\n";
    
    open BINOUT, ">out.bin"
        or die "\nCan't open out.bin for writing: $!\n";
    
    binmode BINOUT;
    
    print BINOUT pack ('B*', $ORed->to_Bin()) ."\n"
    

    Here’s the output

    >perl bitstuff.pl
    bit: 0x1
    byte 2: 0x02
    nineBits: 0x100
    nineBits: 0x100000000
    ORed bit and byte 0x-254
    >cat out.bin 
    \201^@
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two cmake-related problems: first, I can't make it to find the includes
Ok, I have two related lists on my linux box in text files: /tmp/oldList
I have two problems which are related. 1) I have a batch file that
I have two related problems that occur in the following situation. I have a
I have two problems that I'm not sure are related: I have two DropDownList
I have two problems related to my implementation - I need a function which
Hi I have two related problems, i created a pop-up box that appears after
Well I have two problems both related to animation. 1) The following code does
I have two somewhat related problems, shown by the following: user=> (if (symbol? 5)
I have two related problems. Problem 1 : I'm currently using the code below

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.