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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:59:26+00:00 2026-05-23T20:59:26+00:00

I was working on a piece of code to do some compression, and I

  • 0

I was working on a piece of code to do some compression, and I wrote a bitstream class.

My bitstream class kept track of the current bit we are reading and the current byte (unsigned char).

I noticed that reading the next unsigned character from the file was done differently if I used the >> operator vs get() method in the istream class.

I was just curious why I was getting different results?

ex:

this->m_inputFileStream.open(inputFile, std::ifstream::binary);   
unsigned char currentByte;
this->m_inputFileStream >> currentByte;

vs.

this->m_inputFileStream.open(inputFile, std::ifstream::binary);
unsigned char currentByte;
this->m_inputFileStream.get((char&)currentByte);

Additional Info:

To be specific the byte I was reading was 0x0A however when using >> it would read it as 0x6F

I’m not sure how they’re even related ? (they’re not the 2s complement of each other?)

The >> operator is also defined to work for unsigned char as well however (see c++ istream class reference

  • 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-23T20:59:27+00:00Added an answer on May 23, 2026 at 8:59 pm

    If you aren’t parsing text, don’t use operator>> or operator<<. You’ll get weird bugs that are hard to track down. They are also resilient to unit tests, unless you know what to look for. Reading a uint8 for instance will fail on 9 for instance.

    edit:

    #include <iostream>
    #include <sstream>
    #include <cstdint>
    
    void test(char r) {
            std::cout << "testing " << r << std::endl;
            char t = '!';
            std::ostringstream os(std::ios::binary);
            os << r;
            if (!os.good()) std::cout << "os not good" << std::endl;
            std::istringstream is(os.str(), std::ios::binary);
            is >> t;
            if (!is.good()) std::cout << "is not good" << std::endl;
            std::cout << std::hex << (uint16_t)r 
                 << " vs " << std::hex << (uint16_t)t << std::endl;
    }
    
    int main(int argc, char ** argv) {
            test('z');
            test('\n');
            return 0;
    }
    

    produces:

    testing z
    7a vs 7a
    testing 
    
    is not good
    a vs 21
    

    I suppose that would never have been evident a priori.

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

Sidebar

Related Questions

I'm working with a legacy piece of code in some stuff for work which
I have a piece of code working in knockout.js like this: <div> ... some
I am working on some strange piece of code ,For me its not good
I'm working a piece of code to turn phone numbers into links for mobile
I'm working on a piece of code that I inherited and am trying to
I am working on a piece of code which uses regular expressions in c.
I'm currently working in a piece of code where both logic and data access
I'm working on a piece of library code around IDisposable . The managed path
I have this piece of code (summarized)... AnsiString working(AnsiString format,...) { va_list argptr; AnsiString
Why isn't the following piece of code working in IE8? <select> <option onclick=javascript: alert('test');>5</option>

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.