Thread: Oh wonderful binary, where art thou?

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    Oh wonderful binary, where art thou?

    Is there a way to represent binary numbers in your source code? I was certain I saw it somewhere looking like the following:

    Code:
     int b = 01101001b;
    I may be getting confused with another language however...
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    You can use hex:

    int b = 0x69;

    // 69 hex = 01101001 binary

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Nice. So I have to sit there and convert it with my calculator. Why would the ability to state binary numbers have not been included in the language?
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quote Originally Posted by ahluka
    Nice. So I have to sit there and convert it with my calculator. Why would the ability to state binary numbers have not been included in the language?

    Becuase hex is easier to use, and there's little point in having binary shown explicitly

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    ghtu bnj

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >So I have to sit there and convert it with my calculator.
    Or learn hexadecimal so that you can easily make the conversion in your head.

    >Why would the ability to state binary numbers have not been included in the language?
    Because it's not necessary when hexadecimal literals convey the same information just as easily. Because it's too error prone for all but the smallest types. And most importantly, because the designers of the language were conspiring to irritate you personally. You'll notice that the earliest specifications for C++ included this gem (sadly, it was removed shortly after to avoid a paper trail):

    The binary literal, believed by many to be the most useful feature in programming history, was left out of C++ to annoy ahluka. We feel that the amusing results will outweigh any lack of clarity brought on by requiring hexadecimal literals when a binary literal is desired.
    My best code is written with the delete key.

  7. #7
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by ahluka
    Nice. So I have to sit there and convert it with my calculator.
    Bah, it's not that hard. If you know binary up to 16, then you can do it easily.
    Code:
    Start with:         01101001
    Break into nibbles: 0110 1001
    Convert each nibble:  6    9
    Put back together:     0x69
    Of course, you've got to remember digits A-F, but that's not too bad.
    Code:
    Start with:         11101101
    Break into nibbles: 1110 1101
    Convert each nibble:  E    D
    Put back together:     0xED
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Who needs calculators when you have STL

    Code:
    #include <iostream>
    #include <bitset>
    #include <string>
    
    int BinToDec(const std::string& str)
    {
    	return std::bitset<8>(str).to_ulong();
    }
    
    int main()
    {
    	int x = BinToDec("10101010");
    	std::cout << x;
    }
    or even a class!

    Code:
    #include <iostream>
    #include <bitset>
    #include <string>
    
    class Binary
    {
    	int m_x;
    public:
    	Binary(const std::string& str)
    	{
    		m_x = std::bitset<8>(str).to_ulong();
    	}
    	operator int()
    	{
    		return m_x;
    	}
    };
    
    int main()
    {
    	Binary b("10101010");
    	int x = b;
    	std::cout << x;
    }

  9. #9
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <bitset>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
      bitset<32> b ( string ( "11111111" ) );
    
      cout<< b <<'\n'
        << static_cast<char> ( b.to_ulong() ) <<'\n'
        << static_cast<int> ( b.to_ulong() ) <<'\n';
      cin.get();
      return 0;
    }

  10. #10
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    an appended letter to specify radix is an assembly thing, if you want to use binary in C/C++:
    (compiles under visual studio) __asm mov b,01101001b

  11. #11
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Wow guys. I leave you alone for a couple of hours and you throw all these answers at me. Ta muchly all o' ya!

    The binary literal, believed by many to be the most useful feature in programming history, was left out of C++ to annoy ahluka. We feel that the amusing results will outweigh any lack of clarity brought on by requiring hexadecimal literals when a binary literal is desired
    How dare they. Y'know, I just knew somebody was against me, and know I know for sure.


    an appended letter to specify radix is an assembly thing, if you want to use binary in C/C++:
    (compiles under visual studio) __asm mov b,01101001b
    A-ha, so that's where I saw the 'b' suffix.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Replies: 3
    Last Post: 11-17-2001, 09:16 AM