Thread: binary in C++

  1. #1
    Registered User Josh Kasten's Avatar
    Join Date
    Jul 2002
    Posts
    109

    Question binary in C++

    How would I tell the compiler the number is binary?
    ex how would I do this:
    char a=01110101;
    ?

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    why dont you go and try

    (*Hint*You already know how ;P)
    MSVC++~

  3. #3
    Registered User Josh Kasten's Avatar
    Join Date
    Jul 2002
    Posts
    109
    This displays a 9 it should be a 3.
    Code:
    char a=00000011;
    printf("%d",a);
    int a; don't make a program without it.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    I should not reply past 2 am...-_-

    sry for bad info =/
    MSVC++~

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe not what you are looking for but you could look into a bitset as a possibility
    Code:
    #include <iostream>
    #include <bitset>
    #include <string>
    using namespace std;
    
    int main()
    {
        bitset<8> bits1;
        bitset<8> bits2(string("101"));  // One way of initializing a bitset
        cout << "Enter in a value in binary (max 8 bits): ";
        cin >> bits1;                    // Other way of initializing a bitset
        cout << "The binary value " << bits1 << " is equal to " << bits1.to_ulong() << endl;
        cout << "The binary value " << bits2 << " is equal to " << bits2.to_ulong() << endl;
        return 0;
    }
    Outputs:
    Enter in a value in binary (max 8 bits): 0011
    The binary value 00000011 is equal to 3
    The binary value 00000101 is equal to 5
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    You could also consider using a bitfield.

    Code:
    struct bitfield_4_bits
    {
      unsigned int b1: 1;
      unsigned int b2: 1;
      unsigned int b3: 1;
      unsigned int b4: 1;
    };
    Each member is a single bit, accessing the bits can be done the same way as with normal structures. Cconversion from and to a char you can be done using structure mapping.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Don't try to use binary in your source code.

    In your sample line, you are using 01110101 as a constant. In this case it's usually best to do the conversion "manually" and use hex (0x75) in your source code. (Sometimes I'll put the binary value in a comment.) When you get beyond 8 bits, hex is easier (for humans) to read than binary anyway.

    As you may know, all numbers are stored in memory in binary. So, the number-base that you use in the source code is for your convenience only. Any "conversion" is only done during input, output, or compilation.

    If you haven't learned to convert binary to hex yet, Windows has a handy calculator: Start->Programs->Accessories->Calculator [View] [Scientific] [Hex]

    When I want to display a number in binary, I use bitset.

    To get binary input from the user, I input the number as a string, then use strtoul() to convert it to a number.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM