0

Why am I getting an error? It looks pretty straightforward to me. Also, is this the best method for doing what I'm trying to do?

#include <iostream> 
#include <string>


int main() { 

char j = "J"; 
std::cout << bitchar(j);
return 0; 
} 


std::string bitchar(char c) { 
  std::string s;
  unsigned thisdivisor = (unsigned)c;
  while (!thisdivisor) { 
      s += thisdivisor % 2 ? '0' : '1';
      thisdivisor /= 2;
  }
  return s; 
} 
James Hurley
  • 833
  • 1
  • 10
  • 33

5 Answers5

3
#include <iostream> 
#include <string>
#include <bitset>

int main() {   
    char j = 'J'; 
    std::cout << std::bitset<8>(j);
    return 0;  
} 

Note:

  1. "J" is a single character C-style string(with a trailing \0), you should use 'J' for char.
  2. Use std::bitset to print the bit pattern.
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    If he's trying to learn the underlying mechanism, telling him to use a predefined class doesn't really help him. It's like giving a calculator to a child instead of teaching them how to add and multiply. – Barmar Jul 26 '13 at 15:53
  • @Barmar I got your point, but since he also asked if his method is the best, I guess my answer can be useful to him, too. – Yu Hao Jul 26 '13 at 16:01
2

Try char j = 'j' instead of ="j" to assign a character to your variable. "j" is a string array.

Iosif Murariu
  • 2,019
  • 1
  • 21
  • 27
2

Answer 1: Why am I getting these errors

Try char j = 'J', as said by @losifM. The double-quote defines a character array, and you're looking for a single character (single quote).

Answer 2: What's a better way

A better way to do such a thing would be using an std::bitset, then stream it using cout.

//Add this
#include <bitset>

char j = 'j';
std::bitset<8> x(j);
std::cout << x;

Should be self explanatory at that point, but this may help: How to print (using cout) the way a number is stored in memory?

Sidenote:

  s += thisdivisor % 2 ? '0' : '1';

should also be

  s += thisdivisor % 2 ? '1' : '0';

because if thisdivisor % 2 returns 1 (true), you want it to add 1 to s, and vice-versa.

Community
  • 1
  • 1
James Hurley
  • 833
  • 1
  • 10
  • 33
2

You forgot to describe the error. Presumably it's something like

‘bitchar’ was not declared in this scope

because you didn't declare the function before you called it in main. Either move the definition of bitchar before main, or add a declaration before or inside main:

std::string bitchar(char c);

Then you'll probably get something like:

invalid conversion from ‘const char*’ to ‘char’

because you're trying to assign a string literal "J" to a character variable. Use a character literal 'J' (with single quotes) instead.

Then you'll find you're not getting any output. That's because while (!thisdivisor) loops as long as the value is zero; so it won't loop at all if you give it a non-zero value. You want while (thisdivisor) (or while (thisdiviser != 0) if you want to be more explicit), to loop while it's not zero.

Then you'll find that the bits are inverted; you want '0' if the modulo result is zero, while your test gives '0' if it is not zero:

s += thisdivisor % 2 ? '1' : '0';

or

s += (thisdivisor % 2 == 0) ? '0' : '1';

Finally, you might want to reverse the string (or build it by prepending rather than appending) to get the more conventional most-significant-bit-first ordering.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

You are assigning char array (which decay to pointer) to char (J). And then you initialize std::string with char (should be c-string).

Leonid Volnitsky
  • 8,854
  • 5
  • 38
  • 53