views:

99

answers:

4

I've got a homework assignment in my C++ programming class to write a function that outputs the binary value of a variable's value.

So for example, if I set a value of "a" to a char I should get the binary value of "a" output.

My C++ professor isn't the greatest in the whole world and I'm having trouble getting my code to work using the cryptic examples he gave us. Right now, my code just outputs a binary value of 11111111 no matter what I set it too (unless its NULL then I get 00000000).

Here is my code:

#include <iostream>

#define useavalue 1

using namespace std;

void GiveMeTehBinary(char bin);

     int main(){

     #ifdef useavalue
     char b = 'a';
     #else
     char b = '\0';   
     #endif 

     GiveMeTehBinary(b);

     system("pause");

     return 0;

     }

     void GiveMeTehBinary(char bin){

     long s;

     for (int i = 0; i < 8; i++){

         s = bin >> i;

         cout << s%2;

         }

         cout << endl << endl;


     }

Thanks a ton in advance guys. You're always extremely helpful :)

Edit: Fixed now - thanks a bunch :D The problem was that I was not storing the value from the bit shift. I've updated the code to its working state above.

+3  A: 

The compiler should warn you about certain statements in your code that have no effect1. Consider

bin >> i;

This does nothing, since you don’t store the result of this operation anywhere.

Also, why did you declare tehbinary as an array? All you ever use is one element (the current one). It would be enough to store just the current bit.

Some other things:

  • NULL must only be used with pointer values. Your usage works but it’s not the intended usage. What you really want is a null character, i.e. '\0'.
  • Please use real, descriptive names. I vividly remember myself using variables called tehdataz etc. but this really makes the code hard to read and once the initial funny wears off it’s annoying both for you when you try to read your code, and for whoever is grading your code.
  • Formatting the code properly helps understanding a lot: make the indentation logical and consistent.

1 If you’re using g++, always pass the compiler flags -Wall -Wextra to get useful diagnostics about your code.

Konrad Rudolph
I'm using cygwin and it actually doesn't. I'll try your suggestion and see how it goes. Thanks!
Tomoni
It works perfectly now. Thanks so much :)
Tomoni
@Tomoni: If you're using gcc, then compile using `g++ -Wall` to get some useful warnings.
Mike Seymour
Try compiling with -Wall. Also there is no reason for you to have the array tehbinary[8]. Why not just cout in the if statement?
Niki Yoshiuchi
+1  A: 

Try this:

#include <bitset>
#include <iostream>
int main()
{
    std::bitset<8>      x('a');
    std::cout << x << std::endl;
}
Martin York
A: 

Why not just check each bit in the unsigned char variable?

unsigned char b=0x80|0x20|0x01; //some test data
int bitbreakout[8];
if(b&0x80) bitbreakout[7]=1;
//repeat above for 0x40, 0x20, etc.
cout << bitbreakout;

There are a TON of ways to optimize this, but this should give you an idea of what do to.

Coleman
A: 
    #include <iostream>
    using namespace std;
    int main(){
        int x = 255;

        for(int i = numeric_limits<int>::digits; i >=0; i--){
            cout << ((x & (1 << i)) >> i);
        }
    }
Chubsdad