binary number

Discussion in 'C++' started by Bern, Sep 1, 2004.

  1. Bern

    Bern Guest

    how to specifiy a binary number in c++?

    hex numbers are specified by 0x prefix
    Bern, Sep 1, 2004
    #1
    1. Advertising

  2. " Bern" <> wrote...
    > how to specifiy a binary number in c++?
    >
    > hex numbers are specified by 0x prefix


    There is no standard syntax for binary numbers.

    V
    Victor Bazarov, Sep 1, 2004
    #2
    1. Advertising

  3. Bern wrote:

    > how to specifiy a binary number in c++?
    >
    > hex numbers are specified by 0x prefix
    >
    >

    Specifying binary numbers hampers readability of the code. In
    fact, though you can specify octal constants in C++ code ( 0 prefix),
    for all practical purposes use hex numbers since people can understand /
    read it better when written that way.

    --
    Karthik.
    Karthik Kumar, Sep 1, 2004
    #3
  4. " Bern" <> wrote in message news:41355ce2$...
    > how to specifiy a binary number in c++?
    >
    > hex numbers are specified by 0x prefix


    There's no language support for binary literals. Many programmers
    use hexadecimal 0x0001, 0x0002, 0x0004, ... 0x8000 to define bits,
    and then operator|() (a.k.a. operator bit_or) to construct the actual
    number from separate bits.

    Another less common but easier way for some is to use octal (whose
    prefix is plain '0'), which is a bit easier to cope with than hexadecimal
    numbers.

    [By the way, C and C++ are perhaps the only programming languages
    having no token representing decimal zero! :-]

    - Risto -
    Risto Lankinen, Sep 1, 2004
    #4
  5. Bern

    red floyd Guest

    Risto Lankinen wrote:
    >
    > [By the way, C and C++ are perhaps the only programming languages
    > having no token representing decimal zero! :-]
    >
    > - Risto -
    >
    >


    what would the token 0 be, then?
    red floyd, Sep 1, 2004
    #5
  6. Bern

    Mike Wahler Guest

    "red floyd" <> wrote in message
    news:KWmZc.14518$...
    > Risto Lankinen wrote:
    > >
    > > [By the way, C and C++ are perhaps the only programming languages
    > > having no token representing decimal zero! :-]
    > >
    > > - Risto -
    > >
    > >

    >
    > what would the token 0 be, then?


    Octal zero. (it begins with the '0' character).



    -Mike
    Mike Wahler, Sep 1, 2004
    #6
  7. Bern

    Kai-Uwe Bux Guest

    Mike Wahler wrote:

    >
    > "red floyd" <> wrote in message
    > news:KWmZc.14518$...
    >> Risto Lankinen wrote:
    >> >
    >> > [By the way, C and C++ are perhaps the only programming languages
    >> > having no token representing decimal zero! :-]
    >> >
    >> > - Risto -
    >> >
    >> >

    >>
    >> what would the token 0 be, then?

    >
    > Octal zero. (it begins with the '0' character).
    >
    >
    >
    > -Mike


    I am confused?

    What exactly is the difference between the numerical constant 0_8 denoted
    by the string "0" in octal notation versus the numerical constant 0_10
    denoted by the string "0" is decimal notation (as existent in other
    languages). In particular, what is

    0_8 - 0_10


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Sep 1, 2004
    #7
  8. Bern

    Howard Guest

    "Kai-Uwe Bux" <> wrote in message
    news:ch5ea1$6t7$...
    > >> what would the token 0 be, then?

    > >
    > > Octal zero. (it begins with the '0' character).
    > > -Mike

    >
    > I am confused?
    >
    > What exactly is the difference between the numerical constant 0_8 denoted
    > by the string "0" in octal notation versus the numerical constant 0_10
    > denoted by the string "0" is decimal notation (as existent in other
    > languages). In particular, what is
    >
    > 0_8 - 0_10
    > Kai-Uwe Bux


    That's a VERY confusing question. Your terminology and representations are
    difficult to comprehend. But I'll try to address what I THINK you're
    asking:

    There is no difference in the value STORED when you write any if the
    following:

    int hexInt = 0x08;
    int octInt = 08;
    int decInt = 8;

    Those are all representations of the number eight. The only difference is
    what your source code looks like, not what results after compiling.

    The integer literal 0, according to the grammar, is an octal representation
    of the value zero. You could also say 0x0 (or 0x00000000, for that matter),
    using the hexadecimal representation for zero, but it's still the value
    zero. And there IS NO saparate notation for the value zero represented as a
    decimal, because that would be the same as the octal representation, namely
    0. When you write "int x = 0;", you're just assigning the VALUE zero to the
    variable x. It's irrelevant whether the literal 0 is an octal or decimal
    representation. It's still just zero!

    (I suppose, the grammar COULD have been defined so that 00 was the octal
    representation for zero, and 0 was the decimal representation, but that's
    not the way it WAS defined, and it really doesn't matter at all.)

    -Howard
    Howard, Sep 1, 2004
    #8
  9. Bern

    Mike Wahler Guest

    "Kai-Uwe Bux" <> wrote in message
    news:ch5ea1$6t7$...
    > Mike Wahler wrote:
    >
    > >
    > > "red floyd" <> wrote in message
    > > news:KWmZc.14518$...
    > >> Risto Lankinen wrote:
    > >> >
    > >> > [By the way, C and C++ are perhaps the only programming languages
    > >> > having no token representing decimal zero! :-]
    > >> >
    > >> > - Risto -
    > >> >
    > >> >
    > >>
    > >> what would the token 0 be, then?

    > >
    > > Octal zero. (it begins with the '0' character).
    > >
    > >
    > >
    > > -Mike

    >
    > I am confused?


    Perhaps.

    > What exactly is the difference between the numerical constant 0_8 denoted


    0_8 is not a numerical constant. It's not a valid C++ token at all.

    > by the string "0"


    A string is not a numeric literal, it's a string literal.

    > in octal notation


    In C++, octal notation is expressed by the first digit being zero.

    >versus the numerical constant 0_10.


    0_10 is not a numerical constant. It's not a valid C++ token at all.

    > denoted by the string "0"


    A string is not a numeric literal, it's a string literal.

    > is decimal notation


    In C++, decimal notation is expressed by the first digit being
    1, 2, 3, 4, 5, 6, 7, 8, or 9

    >(as existent in other
    > languages).


    This is C++, not other languages.

    >In particular, what is
    >
    > 0_8 - 0_10


    From a C++ perspective, it's gibberish.

    From the C++ standard:

    ================= begin quote ================
    ISO/IEC 14882:1998(E)

    2.13.1 Integer literals

    integer­-literal:
    decimal­-literal integer­-suffix opt
    octal­-literal integer­-suffix opt
    hexadecimal-­literal integer­-suffix opt

    decimal-­literal:
    nonzero-­digit
    decimal-­literal digit

    octal­-literal:
    0
    octal­-literal octal­-digit

    hexadecimal­-literal:
    0x hexadecimal-­digit
    0X hexadecimal-­digit
    hexadecimal-­literal hexadecimal-­digit

    nonzero­-digit: one of
    1 2 3 4 5 6 7 8 9

    octal-­digit: one of
    0 1 2 3 4 5 6 7

    hexadecimal­-digit: one of
    0 1 2 3 4 5 6 7 8 9
    a b c d e f
    A B C D E F

    integer-­suffix:
    unsigned­-suffix long­s-uffix opt
    long­-suffix unsigned­-suffix opt

    unsigned-­suffix: one of
    u U

    long­-suffix: one of
    l L

    1 An integer literal is a sequence of digits that has no period
    or exponent part. An integer literal may have a prefix that
    specifies its base and a suffix that specifies its type. The
    lexically first digit of the sequence of digits is the most
    significant. A decimal integer literal (base ten) begins with
    a digit other than 0 and consists of a sequence of decimal
    digits. An octal integer literal (base eight) begins with the
    digit 0 and consists of a sequence of octal digits. (22)
    A hexadecimal integer literal (base sixteen) begins with 0x
    or 0X and consists of a sequence of hexadecimal digits, which
    include the decimal digits and the letters a through f and A
    through F with decimal values ten through fifteen. [Example:
    the number twelve can be written 12, 014, or 0XC. ]

    2 The type of an integer literal depends on its form, value, and
    suffix. If it is decimal and has no suffix, it has the first of
    these types in which its value can be represented: int, long int;
    if the value cannot be represented as a long int, the behavior
    is undefined. If it is octal or hexadecimal and has no suffix,
    it has the first of these types in which its value can be
    represented: int, unsigned int, long int, unsigned long int.
    If it is suffixed by u or U, its type is the first of these
    types in which its value can be represented: unsigned int,
    unsigned long int. If it is suffixed by l or L, its type is the
    first of these types in which its value can be represented: long
    int, unsigned long int. If it is suffixed by ul, lu, uL, Lu, Ul,
    lU, UL, or LU, its type is unsigned long int. 3 A program is
    ill­formed if one of its translation units contains an integer
    literal that cannot be represented by any of the allowed types.

    (21) The term "literal" generally designates, in this International
    Standard, those tokens that are called "constants" in ISO C.

    (22) The digits 8 and 9 are not octal digits.
    ================= end quote ==================


    The base of literal 0 doesn't really matter, since zero is zero,
    whatever the base.

    -Mike
    Mike Wahler, Sep 2, 2004
    #9
  10. Bern

    josh Guest

    Risto Lankinen wrote:

    > " Bern" <> wrote in message news:41355ce2$...
    >
    >>how to specifiy a binary number in c++?
    >>
    >>hex numbers are specified by 0x prefix


    There were some excellent macros posted to comp.lang.c a while ago:

    http://groups.google.com/groups?selm=&rnum=1

    that let you define binary constants like B8(01001010)

    > Another less common but easier way for some is to use octal (whose
    > prefix is plain '0'), which is a bit easier to cope with than hexadecimal
    > numbers.


    I always found octal harder than hex. :/ I guess mainly 'cuz I'm used
    to grouping my binary numbers in bundles of 4 (or powers of 2) rather
    than bundles of 3 bits.

    > [By the way, C and C++ are perhaps the only programming languages
    > having no token representing decimal zero! :-]


    "0."

    -josh
    josh, Sep 2, 2004
    #10
  11. Bern

    rossum Guest

    On Wed, 01 Sep 2004 22:29:57 GMT, "Howard" <>
    wrote:

    >
    >"Kai-Uwe Bux" <> wrote in message
    >news:ch5ea1$6t7$...
    >> >> what would the token 0 be, then?
    >> >
    >> > Octal zero. (it begins with the '0' character).
    >> > -Mike

    >>
    >> I am confused?
    >>
    >> What exactly is the difference between the numerical constant 0_8 denoted
    >> by the string "0" in octal notation versus the numerical constant 0_10
    >> denoted by the string "0" is decimal notation (as existent in other
    >> languages). In particular, what is
    >>
    >> 0_8 - 0_10
    >> Kai-Uwe Bux

    >
    >That's a VERY confusing question. Your terminology and representations are
    >difficult to comprehend. But I'll try to address what I THINK you're
    >asking:
    >
    >There is no difference in the value STORED when you write any if the
    >following:
    >
    >int hexInt = 0x08;
    >int octInt = 08;
    >int decInt = 8;


    You would have done better to pick 7 or less for the example.
    0x08 == 010 there is no "8" in Octal.

    rossum


    [snip]

    >-Howard
    >
    >
    >
    >
    >
    >


    --

    The ultimate truth is that there is no Ultimate Truth
    rossum, Sep 2, 2004
    #11
  12. Kai-Uwe Bux wrote:
    >
    >
    > I am confused?


    Simple.

    When the compiler sees a number starting with 0, it uses
    the function to parse an octal number for processing
    it. So when the compiler sees a single 0 ....

    Besides: Did you see the smiley in Risto's post ?

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Sep 2, 2004
    #12
  13. Bern

    Howard Guest

    "rossum" <> wrote in message
    news:...

    > >There is no difference in the value STORED when you write any if the
    > >following:
    > >
    > >int hexInt = 0x08;
    > >int octInt = 08;
    > >int decInt = 8;

    >
    > You would have done better to pick 7 or less for the example.
    > 0x08 == 010 there is no "8" in Octal.
    >


    D'OH! :)
    Howard, Sep 2, 2004
    #13
  14. Bern

    Mike Wahler Guest

    "Howard" <> wrote in message
    news:fzGZc.544829$...
    >
    > "rossum" <> wrote in message
    > news:...
    >
    > > >There is no difference in the value STORED when you write any if the
    > > >following:
    > > >
    > > >int hexInt = 0x08;
    > > >int octInt = 08;
    > > >int decInt = 8;

    > >
    > > You would have done better to pick 7 or less for the example.
    > > 0x08 == 010 there is no "8" in Octal.
    > >

    >
    > D'OH! :)


    Should be:

    D'Zero!

    :)

    -Mike
    Mike Wahler, Sep 2, 2004
    #14
  15. Bern

    Bern Guest

    why doesn;t C/c++ support binary numbers?

    " Bern" <> wrote in message news:41355ce2$...
    > how to specifiy a binary number in c++?
    >
    > hex numbers are specified by 0x prefix
    >
    >
    Bern, Sep 3, 2004
    #15
  16. Bern

    Howard Guest

    " Bern" <> wrote in message news:41382a56$...
    > why doesn;t C/c++ support binary numbers?
    >


    I take it you mean "binary integer constants"? (All numbers are stored as
    binary.)

    Well, we have the hex integer constant, which is easier for most people to
    read. Compare (makng up my own format for binary):

    0x8014

    versus

    0_1000000000010100

    It's awfully easy to mis-count the number of zeros there, isn't it? When I
    write such binary-format numbers on paper, I end up breaking them into
    groups, like this:

    1000 0000 0001 0100

    ...which is exactly the hexadecimal grouping! But it's just as easy to say
    0x8014, because I've gotten used to recognizing the bit patterns for hex
    numbers in my brain, and not having to write them down.

    But those are just MY reasons that I don't think it's needed. You'd have to
    ask the standards committee why THEY chose not to use them. :)

    -Howard
    Howard, Sep 3, 2004
    #16
  17. Bern

    Bern Guest

    well, i tought of a good format to use for binary integers.

    0b1011_1001 (equals to dec. 185)

    or

    0_1011_1001

    the first character is a digit zero then followed by a letter 'b'.

    not all people has such a powerful imagination as you.



    "Howard" <> wrote in message
    news:vq%Zc.299794$...
    >
    > " Bern" <> wrote in message news:41382a56$...
    > > why doesn;t C/c++ support binary numbers?
    > >

    >
    > I take it you mean "binary integer constants"? (All numbers are stored as
    > binary.)
    >
    > Well, we have the hex integer constant, which is easier for most people to
    > read. Compare (makng up my own format for binary):
    >
    > 0x8014
    >
    > versus
    >
    > 0_1000000000010100
    >
    > It's awfully easy to mis-count the number of zeros there, isn't it? When

    I
    > write such binary-format numbers on paper, I end up breaking them into
    > groups, like this:
    >
    > 1000 0000 0001 0100
    >
    > ..which is exactly the hexadecimal grouping! But it's just as easy to say
    > 0x8014, because I've gotten used to recognizing the bit patterns for hex
    > numbers in my brain, and not having to write them down.
    >
    > But those are just MY reasons that I don't think it's needed. You'd have

    to
    > ask the standards committee why THEY chose not to use them. :)
    >
    > -Howard
    >
    >
    >
    Bern, Sep 4, 2004
    #17
  18. Bern

    Carl Muller Guest

    rossum <> wrote in message news:<>...
    [snip]
    > You would have done better to pick 7 or less for the example.
    > 0x08 == 010 there is no "8" in Octal.


    I think now is the appropriate time to remind everyone that
    31 OCT == 25 DEC
    which is why programmers get confused about the difference between
    halloween and christmas.
    Carl Muller, Sep 4, 2004
    #18
  19. Bern

    Mike Wahler Guest

    " Bern" <> wrote in message news:41395ca7$...
    > well, i tought of a good format to use for binary integers.
    >
    > 0b1011_1001 (equals to dec. 185)
    >
    > or
    >
    > 0_1011_1001
    >
    > the first character is a digit zero then followed by a letter 'b'.
    >
    > not all people has such a powerful imagination as you.


    It's not a powerful imagination, but simple memorization
    as a result of repetition. After not very long working with
    these number bases, when I see e.g. '1011', I also automatically
    see 'B' (hex) or '11' (decimal). It's really exactly the same
    thing as having memorized in gradeschool the fact that symbol
    42 means 'forty-two'.

    Suggestion: get one of those novelty digital clocks that shows
    the time in BCD (For fun, I wrote a Windows version a while back,
    I'll send it to you if you like). I suspect you'll have it down
    (well, the numbers zero through nine, anyway), in a short time.

    BTW please don't top post.

    -Mike
    Mike Wahler, Sep 5, 2004
    #19
  20. Bern

    alancarre

    Joined:
    May 19, 2013
    Messages:
    1
    Here's my solution. As far as I know it only works up to VC10 (not inclusive)

    ///////////////////////////////////////////////////////////////////////////////
    // Allows assignment or declaration of a variable using a binary expression
    // such as 01010111b
    //
    // Examples:
    // 1. __asm_set(dwBitMask, 11010110b);
    // 2. __asm_def(DWORD dwBitMask, 10110110b);
    //
    ///////////////////////////////////////////////////////////////////////////////
    #if(defined(_MSC_VER) && defined(_M_IX86))
    #define __asm_set(var,val) __asm {mov var,val}
    #define __asm_def(T,var,val) T var;__asm {mov var,val}
    #else
    #define __asm_set(var,val) ASSERT(FALSE)
    #define __asm_def(T,var,val) T var;ASSERT(FALSE)
    #endif

    In a recent example I wanted understand the following line:

    unsigned int setflags(unsigned int newflags, unsigned int mask) {
    unsigned int oldflag = __cur_flag;
    __cur_flag = oldflags & (~mask) | flags & mask;
    return oldflag;
    }

    So I write:

    __asm_set(__cur_flag, 01000010111000011110110000011000b);
    __asm_def(DWORD, dwFlag, 01001000000110000000011001000110b);
    __asm_def(DWORD, dwMask, 11000000110011111000010111001111b);

    setflag(dwFlag, dwMask);

    And printed out the output:
    flags = 0100 1000 0001 1000 0000 0110 0100 0110
    mask = 1100 0000 1100 1111 1000 0101 1100 1111
    oldflags = 0100 0010 1110 0001 1110 1100 0001 1000
    ~mask = 0011 1111 0011 0000 0111 1010 0011 0000
    oldflags & (~mask) = 0000 0010 0010 0000 0110 1000 0001 0000
    flags & mask = 0100 0000 0000 1000 0000 0100 0100 0110
    __cur_flag = 0100 0010 0010 1000 0110 1100 0101 0110

    And if the font is fixed size you can see what's happening.
    alancarre, May 19, 2013
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. makok
    Replies:
    1
    Views:
    6,746
    Anders Hellerup Madsen
    Feb 23, 2004
  2. Fangs
    Replies:
    3
    Views:
    9,759
    darshana
    Oct 26, 2008
  3. Marc Schellens
    Replies:
    8
    Views:
    2,978
    John Harrison
    Jul 15, 2003
  4. Replies:
    12
    Views:
    563
    Richard Heathfield
    Apr 8, 2007
  5. Bob Sanders
    Replies:
    5
    Views:
    206
    Ryan Lewis
    May 30, 2008
Loading...

Share This Page