Velocity Reviews > C++ > binary number

# binary number

rossum
Guest
Posts: n/a

 09-01-2004
On Wed, 01 Sep 2004 22:29:57 GMT, "Howard" <(E-Mail Removed)>
wrote:

>
>"Kai-Uwe Bux" <(E-Mail Removed)> wrote in message
>news:ch5ea1\$6t7\$(E-Mail Removed)...
>> >> 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
>
>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

Karl Heinz Buchegger
Guest
Posts: n/a

 09-02-2004
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
http://www.velocityreviews.com/forums/(E-Mail Removed)

Howard
Guest
Posts: n/a

 09-02-2004

"rossum" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> >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!

Mike Wahler
Guest
Posts: n/a

 09-02-2004

"Howard" <(E-Mail Removed)> wrote in message
news:fzGZc.544829\$(E-Mail Removed)...
>
> "rossum" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
> > >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

Bern
Guest
Posts: n/a

 09-03-2004
why doesn;t C/c++ support binary numbers?

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

Howard
Guest
Posts: n/a

 09-03-2004

" Bern" <(E-Mail Removed)> wrote in message news:41382a56\$(E-Mail Removed)...
> 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
Guest
Posts: n/a

 09-04-2004
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" <(E-Mail Removed)> wrote in message
news:vq%Zc.299794\$(E-Mail Removed)...
>
> " Bern" <(E-Mail Removed)> wrote in message news:41382a56\$(E-Mail Removed)...
> > 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
>
>
>

Carl Muller
Guest
Posts: n/a

 09-04-2004
rossum <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
[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.

Mike Wahler
Guest
Posts: n/a

 09-05-2004
" Bern" <(E-Mail Removed)> wrote in message news:41395ca7\$(E-Mail Removed)...
> 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.

-Mike

alancarre
Junior Member
Join Date: May 2013
Posts: 1

 05-19-2013
Quote:
 Originally Posted by Bern how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
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:
//
///////////////////////////////////////////////////////////////////////////////
#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;
return oldflag;
}

So I write:

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

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.