EmbeddedRelated.com
Forums

Simple C question...entering binary

Started by Thomas Magma January 18, 2008
>> And the most convenient of all >> x = -1;
to get all 1s, better is ~0 -- mac the na�f
On 2008-01-20, Alex Colvin <alexc@TheWorld.com> wrote:

>>> And the most convenient of all >>> x = -1; > > to get all 1s, better is ~0
How is that better? -- Grant Edwards grante Yow! Imagine--a WORLD at without POODLES... visi.com
>> to get all 1s, better is ~0
>How is that better?
should work on 1s-complement machines. -- mac the na&#4294967295;f
Alex Colvin wrote:
> >>> to get all 1s, better is ~0 > >> How is that better? > > should work on 1s-complement machines.
So does "unsigned int u = -1;". -- [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 2008-01-21, Alex Colvin <alexc@TheWorld.com> wrote:
>>> to get all 1s, better is ~0 > >>How is that better? > > should work on 1s-complement machines.
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
Grant Edwards wrote:
> On 2008-01-21, Alex Colvin <alexc@TheWorld.com> wrote: >>>> to get all 1s, better is ~0 >>> How is that better? >> should work on 1s-complement machines. > > 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); >
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).
Thomas Magma wrote:
>> 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. > > >
Maybe you could just write the hex, and do some comments beside it.
On Sun, 27 Jan 2008 17:58:22 +0800, the renowned Tony Winslow
<tonywinslow1986@gmail.com> wrote:

>Thomas Magma wrote: >>> 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. >> >> >> >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
>>>> to get all 1s, better is ~0 >>> How is that better? >> should work on 1s-complement machines.
CBFalconer <cbfalconer@yahoo.com> writes:
> So does "unsigned int u = -1;".
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.
Arlet Ottens <usenet+5@c-scape.nl> writes:
> According to C99, 1's complement versus 2's complement, or even 'sign > and magnitude' notations only apply to _signed_ integer types.
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.
> Unsigned types are represented by straight binary representations > between 0 and 2^N-1.
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.