Binary numbers?

This is a discussion on Binary numbers? within the C++ Programming forums, part of the General Programming Boards category; how can i do this code to display "C" in binary numbers? Code: #include <iostream> using namespace std; int main() ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    44

    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?
    Last edited by Cherry65; 12-16-2008 at 05:44 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    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. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    i dont know. The simplest one. With divisions or bitwise operators.(i dont know what bitmasks are).
    Last edited by Cherry65; 12-16-2008 at 05:51 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    907
    You can also use std::bitset, if that's not overkill.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    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?
    Last edited by Cherry65; 12-16-2008 at 09:31 PM.

  9. #9
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Quote Originally Posted by Cherry65 View Post
    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.

    Quote Originally Posted by Cherry65 View Post
    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. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    44
    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. #12
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. checking if binary numbers are palindromic
    By Beatz in forum C Programming
    Replies: 3
    Last Post: 01-24-2008, 01:49 PM
  2. something about binary numbers in c
    By louis_mine in forum C Programming
    Replies: 1
    Last Post: 02-09-2005, 10:22 PM
  3. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  4. Binary representation of numbers...
    By roc in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2003, 08:42 PM
  5. how to pack 8 x 4 bit binary numbers into a long int?
    By n00bcodezor in forum C Programming
    Replies: 11
    Last Post: 11-19-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21