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

  • Home
  • SEARCH
  • 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 86579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:16:06+00:00 2026-05-10T22:16:06+00:00

This is weird. It is a trivial problem: A std::string with bits with length

  • 0

This is weird. It is a trivial problem:

A std::string with bits with length multiple of 8, the first 8 is: ‘10011100’.

//Convert each 8 bits of encoded string to bytes unsigned char c = 0; for(size_t i = 0; i < encoded.size(); i += 8) {     for(size_t k = 0; k < 8; k++)     {         c <<= k;         if(encoded.at(i + k) == '1') c += 1;          //Debug for first 8 bits         if(i == 0) cout << 'at k = ' << k << ', c = ' << (int)c << endl;     }     outFile.write(reinterpret_cast<char*>(&c), sizeof(char)); } 

Yields the output:

at k = 0, c = 1 at k = 1, c = 2 at k = 2, c = 8 at k = 3, c = 65 at k = 4, c = 17 at k = 5, c = 33 at k = 6, c = 64 at k = 7, c = 0 

Which doesn’t make sense. Shifting 2 places left and getting 8 from 2 is impossible. The max value it can have should be 111b = 7d and in this case should be 100b = 4d.

Enlight me.

  • 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. 2026-05-10T22:16:06+00:00Added an answer on May 10, 2026 at 10:16 pm
    at k = 0, c = 1 at k = 1, c = 2 at k = 2, c = 8 

    That is because:

    input = 10011100 c = 0  `k=0, b=1` shift by 0 add 1 => `c = 1`, dec = 1 `k=1, b=0` shift by 1 add 0 => `c = 10`, dec = 2 `k=2, b=0` shift by 2 add 0 => `c = 1000`, dec = 8 

    b means ‘current bit’. Possibly you don’t want to shift by k, but by 1 ? If you look for a standard C++ solution, you can use std::bitset:

    std::bitset<8> bits('10011100'); unsigned char c = bits.to_ulong(); 

    For your output into the stream, you can use the put function:

    outFile.put(c); 

    It avoids casting pointers and also outputs unformatted (settings like the field-width are ignored).

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

Sidebar

Ask A Question

Stats

  • Questions 67k
  • Answers 67k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Is it not a simple m_NextButton.Enabled = CurrentPos < (TotalCount… May 11, 2026 at 11:51 am
  • added an answer I'm not sure on the syntax in Ruby, but the… May 11, 2026 at 11:51 am
  • added an answer There's no direct way of doing it. Try CommandProxy. Read… May 11, 2026 at 11:50 am

Related Questions

This is weird. It is a trivial problem: A std::string with bits with length
Ok, this is a weird one. The junk data isn't random either, it appears
This is pretty weird. I have my Profiler open and it obviously shows that
This is a weird problem I have started having recently. My team is developing
This one is weird, I have a page that consists of a html table
Ok, this is very weird. I'm trying to do a database migration, and all
This is a really weird problem that I have been having. When I download
This is kind of a weird question and more of an annoyance than technical
This is a bit of a weird one, and I could well be coding
This is the day of weird behavior. We have a Win32 project made with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.