Thread: Binary value declaration

  1. #1
    Registered User
    Join Date
    Jul 2008

    Binary value declaration

    Is it possible to denote a value as binary?

    For example, hex is denoted like so:

    var = 0xFF

    Is there a way to do this for binary values?


  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Only octal and hexadecimal.

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Indeed, working in hexadecimal is working in binary. It's just more compact. (The same could be said of octal...).

    The other solution is to use the strtoul standard function (declared in stdlib.h), for example. That would looks like

    int main()
       unsigned int i = strtoul("11010101110", NULL, 2);
    I hate real numbers.

  4. #4
    Join Date
    Oct 2007
    Inside my computer
    A better way is to use your calculator to convert your binary number into hex, or alternatively, if the calculator does not support such a function, find something on the web that does.
    C does not offer many alternative ways. Doing it at runtime wastes cpu cycles, but it isn't terrible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    I remember being surprised when I found out that you couldn't "directly" use binary in C or C++.

    Most people who work with binary use hex in their programs. (Of, course, the computer doesn't care... Everything is in binary inside the computer... By default, numbers are converted to/from decimal for us during input/output and at compile time.)

    You can fairly easily learn to convert between binary and hex in your head, because each "group" of 4 bits, converts exactly to one hex-digit. You only have to learn 16 conversions and you can convert numbers of any size! (It's much easier than converting between binary and decimal, and you already know zero and one, so you only need to learn 14 more.)

    For example...
    0101 (binary) = 5 (hex) = 5 (decimal)
    0101 0101 (binary) = 55 (hex) = 85 (decimal)
    0101 0101 0101 0101 = 5555 (hex) = 21,845 (decimal)

    And when we do write-out binary numbers, we often put a space between each group of 4 characters as I have done above.

    Hex is much easier (than binary) to read, write, and speak, especially when you go beyond 8 bits.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM