Thread: assiging a binary in C

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    assiging a binary in C

    im trying to figure out how to do it in binary
    int foo = b'0011011;

    or something whats teh syntax for it

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You cannot do this in C like you can with decimal, hex, or octal.
    Last edited by kermit; 09-17-2009 at 05:35 PM.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kermit View Post
    You cannot do this in C like you can with decimal, hex, or octal.
    How about strtol?


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by quzah View Post
    How about strtol?
    A very good reminder. I had completely forgot about that function of strtol (Ironically, I just posted a link on another thread regarding the use of strtol to get integer input). What I had been thinking was something along these lines:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int x = 0xdd; 
        int y = 023;
        int z = b01001;  /* oops - no such beast! */
    
        printf("x = %d\ny = %d\nz = %d\n", x, y, z);
    
        return 0;
    }
    This was something along the lines of what the OP was after I think.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, I had to go look. I couldn't remember if you could do base 2 or not. Apparently anything from 2 to 36 is valid.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Is this what you're trying to get at
    Code:
    int foo = 0x1b;  /* stored inside the machine as binary 0011011 */

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It always seemed kind of strange to me why not offer a direct syntax in C to do what the OP posted

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That and no %b in printf() for printing in binary numbers. I guess the designers of C would be able to read hex numbers as if they were binary, and convert between them in moments.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by dwks View Post
    That and no %b in printf() for printing in binary numbers. I guess the designers of C would be able to read hex numbers as if they were binary, and convert between them in moments.
    That's what I have to do in those situations.

    But it is still strange to me that C didn't include a way to write a binary number.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    gcc has an extension for binary numbers with a 0b prefix. I haven't tried to use it, though. (And of course printf extensions are up to the library people, etc etc etc. Don't know of any binary printers, but there might be some out there.)

  11. #11
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Yep! it would be nice to switch the input base kinda like "bc", which is written in C and uses C-style syntax.

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