[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

binary format of the number.

J.W.

11/27/2008 6:03:00 PM

In c++, we can use hex format to represent a number, for example, for number
70, we can use 0x46, but is there a way to represent a number using the
binary format, something similar to 0x.

Thanks,
J.W.

41 Answers

Erik Wikström

11/27/2008 6:31:00 PM

0

On 2008-11-27 19:03, J.W. wrote:
> In c++, we can use hex format to represent a number, for example, for number
> 70, we can use 0x46, but is there a way to represent a number using the
> binary format, something similar to 0x.

No, but you can use octal if you like, all numbers starting with 0 are
considered to be in octal form.

--
Erik Wikström

Sherm Pendley

11/27/2008 6:34:00 PM

0

"J.W." <jsunnewsgroup@gmail.com> writes:

> In c++, we can use hex format to represent a number, for example, for
> number 70, we can use 0x46, but is there a way to represent a number
> using the binary format, something similar to 0x.

Oddly enough, no there isn't. We can declare hex-format literals with a
leading 0x, and octal-format with a leading 0, but there's no standard
way to declare a binary-format literal.

That's always seemed to me a strange thing to omit from the language.

sherm--

--
My blog: http://shermspace.bl...
Cocoa programming in Perl: http://camelbones.sourc...

Bill

11/27/2008 7:32:00 PM

0


"J.W." <jsunnewsgroup@gmail.com> wrote in message
news:492ee0d4$0$90269$14726298@news.sunsite.dk...
> In c++, we can use hex format to represent a number, for example, for
> number 70, we can use 0x46, but is there a way to represent a number using
> the binary format, something similar to 0x.
>
> Thanks,
> J.W.

0x is designed as a shortcut--so that you don't have to do that, sort of
like specifiying C-style character strings with double-quotes. "Octal" is
also provided, as you may know, by omitting the x in 0x.

Why would you prefer to write down strings of 0s and 1s anyway? Seems like
it would increase your chance of error. If you really wanted to, you could
write your own function which translates binary character strings to an
integral data type. For instance, int binary2Int(const char *).

Bill


ram

11/27/2008 11:15:00 PM

0

Sherm Pendley <spamtrap@dot-app.org> writes:
>We can declare hex-format literals with a leading 0x

The verb »declare« already has a specific meaning in ISO/IEC
14882:2003(E), but this meaning does not apply in this case.

ram

11/27/2008 11:20:00 PM

0

=?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= <Erik-wikstrom@telia.com> writes:
>all numbers starting with 0 are
>considered to be in octal form.

Numbers like »0.24« or »0x24«?

(Actually, numbers never start
with »0«, but literals might.)

Tarmo Kuuse

11/28/2008 9:19:00 AM

0

Bill wrote:
> Why would you prefer to write down strings of 0s and 1s anyway? Seems like
> it would increase your chance of error. If you really wanted to, you could
> write your own function which translates binary character strings to an
> integral data type. For instance, int binary2Int(const char *).

When working at low level (embedded, device drivers, ...), bit fields
are scattered throughout code. It is quite annoying to convert binary to
hexadecimal and vice versa for 32-bit values.

Let's see now, is bit 22 set in mask 0x03C00000? OK, give me a minute...

--
Tarmo

blargg.h4g

11/28/2008 3:47:00 PM

0

Tarmo Kuuse wrote:
> Bill wrote:
> > Why would you prefer to write down strings of 0s and 1s anyway? Seems like
> > it would increase your chance of error. If you really wanted to, you
could
> > write your own function which translates binary character strings to an
> > integral data type. For instance, int binary2Int(const char *).
>
> When working at low level (embedded, device drivers, ...), bit fields
> are scattered throughout code. It is quite annoying to convert binary to
> hexadecimal and vice versa for 32-bit values.
>
> Let's see now, is bit 22 set in mask 0x03C00000? OK, give me a minute...

Is bit 22 set in this mask? OK, give me a minute...

00000011110000000000000000000000

How about this one? Simple, yes.

(1L << 25) | (1L << 24) | (1L << 23) | (1L << 22)

If you really want binary, you can write a macro (or template) that
accepts four 8-bit chunks, something like
BIN(00000011,11000000,00000000,00000000).

Hendrik Schober

11/28/2008 7:41:00 PM

0

J.W. wrote:
> In c++, we can use hex format to represent a number, for example, for number
> 70, we can use 0x46, but is there a way to represent a number using the
> binary format, something similar to 0x.

ISTR that boost has some template that yields an integer
const from a string of o and 1. ICBWT.

> J.W.

Schobi

Bill

11/28/2008 10:52:00 PM

0


"Tarmo Kuuse" <tarmo.kuuse@mail.ee> wrote in message
news:ggod2m$3ad$1@aioe.org...
> Bill wrote:
>> Why would you prefer to write down strings of 0s and 1s anyway? Seems
>> like it would increase your chance of error. If you really wanted to,
>> you could write your own function which translates binary character
>> strings to an integral data type. For instance, int binary2Int(const
>> char *).
>
> When working at low level (embedded, device drivers, ...), bit fields are
> scattered throughout code. It is quite annoying to convert binary to
> hexadecimal and vice versa for 32-bit values.
>
> Let's see now, is bit 22 set in mask 0x03C00000? OK, give me a minute...
>
> --
> Tarmo

I think I would be MUCH less likely to make an error typing in hexadecimal
strings. The idea of typing in a string with a single 1 in the 22nd place
gives me a headache just thinking about it. I would need to
quadruple-check it. In hex, I would only need to double-check it.
Depending on the storage requirements I might prefer an array whose members
of type bool---then there is no ambiguity about what is meant by the 22nd
bit.

Bill


Kai-Uwe Bux

11/28/2008 11:10:00 PM

0

Bill wrote:

>
> "Tarmo Kuuse" <tarmo.kuuse@mail.ee> wrote in message
> news:ggod2m$3ad$1@aioe.org...
>> Bill wrote:
>>> Why would you prefer to write down strings of 0s and 1s anyway? Seems
>>> like it would increase your chance of error. If you really wanted to,
>>> you could write your own function which translates binary character
>>> strings to an integral data type. For instance, int binary2Int(const
>>> char *).
>>
>> When working at low level (embedded, device drivers, ...), bit fields are
>> scattered throughout code. It is quite annoying to convert binary to
>> hexadecimal and vice versa for 32-bit values.
>>
>> Let's see now, is bit 22 set in mask 0x03C00000? OK, give me a minute...
>>
>> --
>> Tarmo
>
> I think I would be MUCH less likely to make an error typing in hexadecimal
> strings. The idea of typing in a string with a single 1 in the 22nd
> place
> gives me a headache just thinking about it. I would need to
> quadruple-check it. In hex, I would only need to double-check it.
[snip]

I agree, but that is only because we are dealing with 32bit numbers. With
8bit numbers, things are different. I can see which bits are set in

01001110

right away. With a hex number such as

d3

I have to think, which is a BadThing(tm).


Best

Kai-Uwe Bux