Thread: Binary representation similar to 0xFF ?

  1. #1
    Hardware Engineer
    Join Date
    Sep 2001

    Binary representation similar to 0xFF ?

    Is there a binary representation similar to the way hex and octal numbers are represented?

    I usually do this:
    int MyMask = 0x08 ; // Hex mask format - OK

    Is there something like this?
    int MyMask = 0b1000 ; // Binary mask format - Wrong!

    Im I correct that there is nothing like this in C++ ? I'm shocked that this doesn't seem to exist!

    I don't have any problems converting between binary and hex, but I thought it might make the code more readable sometimes.

    Ref: Primary compiler = MSVC++

  2. #2
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    There is no way to write binary literals in C++. They are long, that's probably why they aren't used.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    if you plan to make bit flags to find out if a certain bit is selected, declare an array of bit flags in your header file.

    int bitFlag[8]={1,2,4,8,16,32,64,128};
    That will enable you to get a bit status from a single byte variable. for use with shorts and longs, just extend the array to compensate. heres a function that i include in all my programs.

    int GetBit(BYTE byte,int bit)
    	if ( (bit<0)||(bit>7) )
    		Notify(NULL,"bit request error; \nmust be between 0 and 7");
    	if (byte&bitFlag[bit])
    		return 1;
    	return 0;
    dont steal my code, but learn from it if you need to. i doubt you'll need to, im probably not even talking about the same problem as you.
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001



    Yeah, SilentStrike, anything longer than a byte and you have to start counting bit-positions. But, I work with bytes a lot (hardware stuff).

    I know I could write a function to do this, but I wanted to make the code more readable by making it easier to see which bits are being manipulated & tested. Sometimes I put the binary in a comment, or just make a comment like "If bit 3 is high". Adding a function (in this particular case) would make it more confusing. And other programmers would laugh at me saying "Why don't you just use hex?"

    bennyandthejets: Good solution. There might me a time when something like this is better than using hex... (Hmmm....)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. best STL method to implement a binary tree
    By MatthewDoucette in forum C++ Programming
    Replies: 8
    Last Post: 06-16-2006, 07:08 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. - signed int to binary?
    By KnowledgeHungry in forum C++ Programming
    Replies: 7
    Last Post: 08-25-2003, 11:57 AM