Binary value declaration

This is a discussion on Binary value declaration within the C Programming forums, part of the General Programming Boards category; Is it possible to denote a value as binary? For example, hex is denoted like so: [code] var = 0xFF ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    2

    Binary value declaration

    Is it possible to denote a value as binary?

    For example, hex is denoted like so:

    [code]
    var = 0xFF
    [code]

    Is there a way to do this for binary values?

    Thanks!

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

  3. #3
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    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

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

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  5. #5
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    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, 02:49 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21