-2

I have this C style program:

void showOneByteBinaryNumber( char c ) {
    for ( int i = 128; i >=1; i >>= 1 ) {
        // Fixed Typo - Replaced a ; with the correct {
        if ( c & i ) {
            printf( "1" );
        } else {
            printf( "0" );
        }
    }
}

int main() {
    for ( int i = 0; i < 256; i++ ) {
        printf( "%3d = ", i );
        showOneByteBinaryNumber( i );
        printf( "\n" );
     }

     return 0;
}

I then change my program to this:

void showOneByteBinaryNumber( char c ) {
    for ( int i = 128; i >= 1; i >>= 1 ) {
        if ( c & i ) {
            std::cout << 1;
        }
        else {
            std::cout << 0;
        }
    }
}

int main() {
    for ( int i = 0; i < 256; i++ ) {
        std::cout << "%3d = " << i;
        showOneByteBinaryNumber( i );
        std::cout << "\n";
    }

    return 0;
}

I should be expecting to see the table of binary values increasing from 0 to 255, however when I try to convert this to a c++ equivalent version using std::cout instead, I'm getting other digits that are not 0 or 1 in the output.

I'm not sure if the culprit is within the changed function or within the for loop within main. I'm not sure how to change the parameter for printf() "%3d"; to do the same with std::cout

Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • for setting width use `std::setw()` function defined in `iomanip` library – The Philomath Aug 11 '16 at 03:57
  • @RamandeepPunia I did try that but it didn't seem to work as expected; I used a value of 3 with std::setw and I'm still getting the same results – Francis Cugler Aug 11 '16 at 03:57
  • 1
    `std::cout << "%3d = "` doesn't do what you think it does. – John3136 Aug 11 '16 at 03:59
  • @John3136 I do know that; and I've been trying to use the `std::(manipulators)` but to no avail I'm still getting the same results – Francis Cugler Aug 11 '16 at 04:00
  • 1
    The `if ( c & i );` missing an opening `{` in the c version – sjsam Aug 11 '16 at 04:00
  • @sjsam ty for pointing out the typo – Francis Cugler Aug 11 '16 at 04:01
  • Already a down vote and no explanation as to why they downvoted... damn trolls. – Francis Cugler Aug 11 '16 at 04:04
  • 1
    @sjsam i fixed the typo; ty for bring that to my attention; but that is not the problem; the code is correct in my editor – Francis Cugler Aug 11 '16 at 04:05
  • 1
    @FrancisCugler Maybe you got downvoted due to typing in code instead of *copying and pasting* from your editor into the SO edit window. What is `ShowOneByeBinaryNumber`? See, another typo. – PaulMcKenzie Aug 11 '16 at 04:07
  • @John3136 if you read the last sentence of my question you will see that I'm not sure how to change the param for printf() that is %3d to do the same behavior with cout. – Francis Cugler Aug 11 '16 at 04:07
  • @PaulMcKenzie yeah I don't have my actual IDE on this pc; it is on my other pc so copy & paste isn't exactly an option at the moment – Francis Cugler Aug 11 '16 at 04:09
  • 1
    @FrancisCugler See [this answer](http://stackoverflow.com/a/11226376/4520911). I stand by [PaulMcKenzie](http://stackoverflow.com/users/3133316/paulmckenzie)...the down vote could have been because typos in the question look like you were careless when asking (not saying that you were, but it makes it look like you did not put effort into the question). – iRove Aug 11 '16 at 04:09
  • 2
    @FrancisCugler Then use something like http://ideone.com (there are others too) to verify your code. Note, still copy-paste the code to the SO question, instead of just linking to code in a web IDE. – hyde Aug 11 '16 at 04:12
  • 1
    @FrancisCugler Perhaps the downvote is because your C++ version is a waste of space - you know it is wrong. Even your C is pretty pointless. Seems your question is just "How can I do the equivalent of `printf("%3d", i)` using C++ streams?. – John3136 Aug 11 '16 at 04:14
  • 1
    @FrancisCugler For me it is working fine if i replace the line `std::cout << "%3d = " << i; ` with `std::cout < – The Philomath Aug 11 '16 at 04:16
  • @RamandeepPunia Thank You; I was on the right track the only thing that was throwing me off was that I needed to swap the `i` and the `" = "` around the stream insertion operators. – Francis Cugler Aug 11 '16 at 04:21
  • @hyde ty for the website; first time I've heard of it; very nice tool; I'll have to remember that. – Francis Cugler Aug 11 '16 at 04:22
  • @PaulMcKenzie; Well it is two fold; first my IDE is on my other PC; so I was manually typing it and there were typos that I fixed when others noticed them, and it wasn't that I wasn't putting effort into it; I was in a hurry for a quick solution cause I was trying to use this function as a reference to another website where it was relevant to the topic. I can understand the down votes in which I don't have a problem; but if people are going to down vote they can at the least state that they did and explain why the why the down vote. That way I can improve on future questions and answers! – Francis Cugler Aug 11 '16 at 04:53
  • @hyde yeah; I don't link my code; I either copy & paste from my IDE when I have it available or I type it hard code when I don't and unfortunately I do get typos from time to time when I hard type it. – Francis Cugler Aug 11 '16 at 05:10
  • You can use `printf` in c++, IMHO there's no point changing something that works, just for the sake of it – M.M Aug 11 '16 at 05:52
  • @M.M I know that printf still works in c++; I'm just converting all of my older libraries to use the stl. – Francis Cugler Aug 11 '16 at 16:36
  • iostream isn't normally considered to be part of "the stl" – M.M Aug 11 '16 at 21:25
  • @M.M yeah true; but it has the same feel as the STL and does reside in the same namespace! That and if I'm not doing a "C" program; I want to try to stay as far way as possible from the "C - Standard Library functions". Same as for using the file functions. – Francis Cugler Aug 12 '16 at 21:44

1 Answers1

3

It was RamandepPunia who provided the correct answer for me!

In this line of code:

printf( "%3d", i );

where I was trying to replace it with

std::cout << "%3d = " << i; // I did initially try std::setw( 3 ) but was giving wrong results;
std::cout << std::setw(3) << " = " << i;

His comment with the code:

std::cout << std::setw(3) << i << " = ";

Is what I was looking for and now the program gives me the correct results!

EDIT

After I got it working I did modify my function to this:

void showOneByteBinaryNumber( char c ) {
    for ( int i = 128; i >= 1; i >>= 1 ) {
        std::cout << ( (c & i) ? 1 : 0);
     }
}

However: David C. Rankin showed me this version as well

void showOneByteBinaryNumber( char c ) {
    for ( int i = 1 << 7; i; i >>= 1 ) {
        c & i ? std::cout << 1 : std::cout << 0;
    }
}
Francis Cugler
  • 7,788
  • 2
  • 28
  • 59
  • hmm, you could replace the if part with `printf("%c",(c&i)?'1':'0');`, but why bother? ;) – sjsam Aug 11 '16 at 04:39
  • 1
    Also instead of hardcoding `128`, it would be good if you could programmatically find it. – sjsam Aug 11 '16 at 04:42
  • @sjsam well it was a crude old c program to give a quick list of the binary sequence of digits from [0,255]. I was using it as a code snippet for another site; but I needed to convert it real fast to c++; the std::setw() function was right but the order that I was using the `i` and `" = "` in the `cout` statement was throwing me off. – Francis Cugler Aug 11 '16 at 04:50
  • 2
    You can rework `showOneByteBinaryNumber( char c )` to contain nothing but `for ( int i = 1 << 7; i; i >>= 1 ) c & i ? std::cout << 1 : std::cout << 0;` using the *ternary* operator for brevity. – David C. Rankin Aug 11 '16 at 04:50
  • @DavidC.Rankin In my IDE after I got the C++ version working correctly I did replace the `if ... else` with the conditional operator, but I didn't change the for loop itself. – Francis Cugler Aug 11 '16 at 04:57