Quick question.
How do you enter a literal in binary? That is, the binary equivalent of:
x = 0xFFFF //hex entry
Thank, Thomas
Quick question.
How do you enter a literal in binary? That is, the binary equivalent of:
x = 0xFFFF //hex entry
Thank, Thomas
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
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.
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
There is a clever set of macros from Tom Torfs to do this. See:
Roberto Waltman
[ Please reply to the group, return address is invalid ]
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]: Try the download section.
Why not. I'd say a decent compiler will just calculate it at compile time.
-- 42Bastian Do not email to bastian42@yahoo.com, it's a spam-only account :-) Use @monlynx.de instead !
"Thomas Magma" skrev i meddelandet news:Yw7kj.100328$EA5.10420@pd7urf2no...
And here is the ANSI C compatible way.
#define BIT(n) (1
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;
--
-- 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
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]: Try the download section.
to get all 1s, better is ~0
-- mac the naïf
How is that better?
-- Grant Edwards grante Yow! Imagine--a WORLD at without POODLES... visi.com
should work on 1s-complement machines.
-- mac the naïf
So does "unsigned int u = -1;".
-- [mail]: Chuck F (cbfalconer at maineline dot net) [page]: Try the download section.
Yes, I gues it should. Anybody have a 1's compliment machine handy to see what they get when the do this?
unsigned u; u = -1; printf("%x",u);
-- Grant Edwards grante Yow! I'm shaving!! at I'M SHAVING!! visi.com
According to C99, 1's complement versus 2's complement, or even 'sign and magnitude' notations only apply to _signed_ integer types.
Unsigned types are represented by straight binary representations between 0 and 2^N-1.
When converting negative numbers from signed to unsigned, this is done by repeatedly adding or subtracting 1. In other words, to convert -1 to unsigned, you take 0U, and subtract 1, which is guaranteed to produce an all-ones bit pattern (ignoring pad bits).
Maybe you could just write the hex, and do some comments beside it.
Here is another way.
// Control signal pin direction #define RW_TRIS TRISBbits.TRISB11 #define RS_TRIS TRISAbits.TRISA6 #define E_TRIS TRISAbits.TRISA7
Then just set/clear them one at time. Not efficient, I know, but very clear, and it makes little difference in the context of a large program.
You can also create #defines and OR or AND them, as for the configuration words
(the below wrapped, sorry)
#define TRPA 0xFFF0 // Protect All Blocks excluding Boot #define TRPALL 0xBFF0 // Protect All Blocks including Boot #define TRP3 0xFFF7 // Protect Block 3 (6000-7FFF) #define TRP2 0xFFFB // Protect Block 2 (4000-5FFF)
Best regards, Spehro Pefhany
-- "it's the network..." "The Journey is the reward" speff@interlog.com Info for manufacturers: http://www.trexon.com Embedded software/hardware/analog Info for designers: http://www.speff.com
On a 1's complement machine, that will result in the variable u having all bits set other than the least signficant bit, since that is the one's complement representation of -1.
Sure. So your -1 literal is a one's complement signed literal, represented with all bits one except the LSB. Now you assign it to your unsigned variable, causing an implicit cast, and you still have the same bit pattern as your signed constant.
Sure. But the implict cast doesn't change the representation, only the interpretation. Now instead of -1, the bit pattern represents the maximum unsigned integer minus 1.
ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.