1. ## Binary numbers?

how can i do this code to display "C" in binary numbers?
Code:
```#include <iostream>

using namespace std;

int main()
{
unsigned char A = 0xF8;
unsigned char B = 0x5A;
unsigned int C;
C = ~A;
cout << C << endl;
C = ~B;
cout << C << endl;
C = A<<2;
cout << C << endl;
C = A>>3;
cout << C << endl;
C = B<<1;
cout << C << endl;
C = B>>4;
cout << C << endl;
system("PAUSE");
}```
and also, how can i convert a binary number from a decimal number, which was inputted by the user?How do i display that binary number as a string?

2. You do realize that all your numbers are stored internally in binary, right? You don't need to convert anything, because the value is going to be the same.

In terms of getting it to print out in binary, well, you have to do that (meaning you have to write code to do so).

3. Ya i know i have to wrtite a code, but i dont know how to do it. Can you give me at list a clue?

4. There's lots of ways to do it. You can do successive divisions. You can use bitwise operators to peel things off one bit at a time. You can use bitmasks. How do you want to do it?

5. i dont know. The simplest one. With divisions or bitwise operators.(i dont know what bitmasks are).

6. Well, then you just need to know "is the right-most bit of this number 1 or 0" and "throwing away the right-most bit of this number and moving everything else down one spot".

7. You can also use std::bitset, if that's not overkill.

8. well i did it like this:
Code:
```#include <iostream>
#include <string>
#include <cmath>

using namespace std;

int main()
{
cout << "Enter a decimal number: ";
int inum = 0;
cin >> inum;

string result = "";

int num = inum;
for(float i=16; i>=0;i--)
{
if(num - (powf(2.0f, i)) >= 0)
{
num -= powf(2.0f, i);
result += '1';
}
else if(result.length()>0)
{
result += '0';
}
}

cout << inum << " = " << result << " in binary. \n";
system("PAUSE");
}```
is that a good way of doing it? it works with all numbers (in the int range).

and one more thing, how can i input a number and store it in an unsigned char?cause in my book says it is possible, but when i input the char the compiler takes the first char, and the value of that char in int is its value in ASCII. is there any way i can input a number and store it in a char?

9. Originally Posted by Cherry65
is that a good way of doing it? it works with all numbers (in the int range).
Really? I think you'll find that for every number over 131070 it gets the same answer.

Originally Posted by Cherry65
and one more thing, how can i input a number and store it in an unsigned char?cause in my book says it is possible, but when i input the char the compiler takes the first char, and the value of that char in int is its value in ASCII. is there any way i can input a number and store it in a char?
I don't understand what you're asking here.

10. If you use >> to read in an unsigned char, the compiler will assume, as silly as it sounds, that you intend to read in a char. If that is not true (that is you want to type 87 and have the unsigned char have the value 87), then you'll have to read into an int or whatever and then assign that value into the char.

11. oh so ill have to use an int first. thank you very much!

and stiltskin, the script works fine, you should compile it and check.

12. He meant that the int range is not necessarily limited to 2 bytes (16 bits). In fact, I believe with modern compilers 32-bit integers are much more common.