There are 46 messages in this thread.
You are currently looking at messages 1 to 10.
So far in May, you have voted 0 times ou of a total of 20 votes by the community.
Please help us clean the archives from unuseful discussion threads by using the voting system! Details here.
Quick question. How do you enter a literal in binary? That is, the binary equivalent of: x = 0xFFFF //hex entry Thank, Thomas
Thomas Magma wrote: > Quick question. > > How do you enter a literal in binary? That is, the binary equivalent of: > > x = 0xFFFF //hex entry x = 0xFFFF; I have seen some compilers with non-standard extensions that allow the similar form x = 0b11111111; but that IS non-standard. Probably if one really, really wanted to do that, the source could be run through a Perl or awk filter before handing it off to the pre-processor. Also once saw (and was amazed by) a page of pre-processor macros that took a "binary" argument and emitted the equivalent hex form. -- Rich Webb Norfolk, VA
> > x = 0b11111111; > The C30 compiler excepted that, thanks. (I'm programming a dsPIC for the first time and I like to set my TRIS register values in binary.) Thanks Rich.
Thomas Magma wrote:
> How do you enter a literal in binary? That is, the binary equivalent of:
>
> x = 0xFFFF //hex entry
You translate it to hex and use that :-) Or, you define mnemonic names
for the actual bits you're using and use these in an expression, as in
GCTL = TXEN | RXEN | STOP_BIT | WORD_SIZE_8
instead of
GCTL = 0b00110101;
(register names have been made up).
That aside, the preprocessor magic version is a variation of
#define BINARY_OCTET(x) \
(((0##x & 01)) | \
((0##x & 010) >> 2) | \
((0##x & 0100) >> 4) | \
((0##x & 01000) >> 6) | \
((0##x & 010000) >> 8) | \
((0##x & 0100000) >> 10) | \
((0##x & 01000000) >> 12) | \
((0##x & 010000000) >> 14))
int main() {
printf("%x\n", BINARY_OCTET(10100100));
return 0;
}
It's fun for its hack value, but I wouldn't want it in production code.
Stefan
"Thomas Magma" wrote: >How do you enter a literal in binary? That is, the binary equivalent of: >x = 0xFFFF //hex entry There is a clever set of macros from Tom Torfs to do this. See: http://www.velocityreviews.com/forums/t317511-binary-constant-macros.html Roberto Waltman [ Please reply to the group, return address is invalid ]
Thomas Magma wrote:
>
> How do you enter a literal in binary? That is, the binary
> equivalent of: x = 0xFFFF //hex entry
You can enter the value in hex, octal, or decimal. For example:
unsigned int x;
....
x = 0xffff;
/* or */ x = 0177777;
/* or */ x = 65535U;
/* or */ x = 0170000 + 0xf00 + 255U;
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com
On Fri, 18 Jan 2008 21:08:21 +0100, Stefan Reuther
<s...@arcor.de> wrote:
>That aside, the preprocessor magic version is a variation of
> #define BINARY_OCTET(x) \
> (((0##x & 01)) | \
> ((0##x & 010) >> 2) | \
> ((0##x & 0100) >> 4) | \
> ((0##x & 01000) >> 6) | \
> ((0##x & 010000) >> 8) | \
> ((0##x & 0100000) >> 10) | \
> ((0##x & 01000000) >> 12) | \
> ((0##x & 010000000) >> 14))
>
> int main() {
> printf("%x\n", BINARY_OCTET(10100100));
> return 0;
> }
>It's fun for its hack value, but I wouldn't want it in production code.
Why not. I'd say a decent compiler will just calculate it at compile
time.
--
42Bastian
Do not email to b...@yahoo.com, it's a spam-only account :-)
Use <same-name>@monlynx.de instead !
"Thomas Magma" <s...@overtherainbow.com> skrev i meddelandet news:Yw7kj.100328$EA5.10420@pd7urf2no... > > >> x = 0b11111111; >> > > The C30 compiler excepted that, thanks. (I'm programming a dsPIC for the > first time and I like to set my TRIS register values in binary.) > > Thanks Rich. > > And here is the ANSI C compatible way. #define BIT(n) (1 << n) #define BIT0 BIT(0) #define BIT1 BIT(1) #define BIT2 BIT(2) #define BIT3 BIT(3) #define BIT4 BIT(4) #define BIT5 BIT(5) #define BIT6 BIT(6) #define BIT7 BIT(7) TRIS = BIT7 | BIT3 | BIT0; -- Best Regards, Ulf Samuelsson This is intended to be my personal opinion which may, or may not be shared by my employer Atmel Nordic AB
In article <4...@yahoo.com>,
CBFalconer <c...@maineline.net> wrote:
>Thomas Magma wrote:
>>
>> How do you enter a literal in binary? That is, the binary
>> equivalent of: x = 0xFFFF //hex entry
>
>You can enter the value in hex, octal, or decimal. For example:
>
> unsigned int x;
> ....
> x = 0xffff;
>/* or */ x = 0177777;
>/* or */ x = 65535U;
>/* or */ x = 0170000 + 0xf00 + 255U;
And the most convenient of all
x = -1;
(For those not in the know, the definition of unsigned
explicitly makes this works. There may be an additional
advantage that it ports over to 64 bits more easily.)
And of course:
x = ~0;
>
>--
> [mail]: Chuck F (cbfalconer at maineline dot net)
> [page]: <http://cbfalconer.home.att.net>
> Try the download section.
>
>
>
>--
>Posted via a free Usenet account from http://www.teranews.com
>
--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
Albert van der Horst wrote: > CBFalconer <c...@maineline.net> wrote: >> Thomas Magma wrote: >>> >>> How do you enter a literal in binary? That is, the binary >>> equivalent of: x = 0xFFFF //hex entry >> >> You can enter the value in hex, octal, or decimal. For example: >> >> unsigned int x; >> .... >> x = 0xffff; >> /* or */ x = 0177777; >> /* or */ x = 65535U; >> /* or */ x = 0170000 + 0xf00 + 255U; > > And the most convenient of all > x = -1; > > (For those not in the know, the definition of unsigned > explicitly makes this works. There may be an additional > advantage that it ports over to 64 bits more easily.) Depends on the objective. x = -1 will create the maximum unsigned value possible (which might be 0xffffffff on some machines). The sequences I showed will create 0xffff, regardless of the size of the int. -- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: <http://cbfalconer.home.att.net> Try the download section. -- Posted via a free Usenet account from http://www.teranews.com