Sign in

username:

password:



Not a member?

Search Comp.Arch.Embedded



Search tips

embedded by Keywords

68HC11 | 68HC12 | 8051 | 8052 | ARM | ARM7 | Asic | AT91 | AT91RM9200 | Atmel | AVR | AVRStudio | Bootloader | CFP | CompactFlash | Cygnal | Cypress | Dataflash | DSP | eCos | EEPROM | Embedded Linux | Emulator | Endian | Ethernet | Firewire | FPGA | Freescale | GCC | GNUARM | GSM | H8 | HDLC | I2C | Infineon | Interrupts | Java | JTAG | LCD | LED | LPC2000 | MCU | Microchip | MMC | MPLAB | MSP430 | PC104 | PCB | PCI | PCMCIA | PowerPC | Rabbit | RS232 | RS485 | RTOS | SBC | SDRAM | Sensor | SPI | STK500 | UART | UML | USART | USB | Verilog | VHDL | VxWorks | Xilinx

Ads

Discussion Groups

Discussion Groups | Comp.Arch.Embedded | Simple C question...entering binary

There are 45 messages in this thread.

You are currently looking at messages 10 to 20.

Re: Simple C question...entering binary - Alex Colvin - 09:19 20-01-08

>> And the most convenient of all
>>            x = -1;
to get all 1s, better is ~0
-- 
	mac the naïf



Re: Simple C question...entering binary - Grant Edwards - 10:17 20-01-08

On 2008-01-20, Alex Colvin <a...@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            

Re: Simple C question...entering binary - Alex Colvin - 23:18 20-01-08

>> to get all 1s, better is ~0

>How is that better?

should work on 1s-complement machines.

-- 
	mac the naïf

Re: Simple C question...entering binary - CBFalconer - 00:15 21-01-08

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


Re: Simple C question...entering binary - Grant Edwards - 11:21 21-01-08

On 2008-01-21, Alex Colvin <a...@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            

Re: Simple C question...entering binary - Arlet Ottens - 11:37 21-01-08

Grant Edwards wrote:
> On 2008-01-21, Alex Colvin <a...@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).

Re: Simple C question...entering binary - Tony Winslow - 04:58 27-01-08

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.

Re: Simple C question...entering binary - Spehro Pefhany - 10:44 27-01-08

On Sun, 27 Jan 2008 17:58:22 +0800, the renowned Tony Winslow
<t...@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"
s...@interlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com

Re: Simple C question...entering binary - Eric Smith - 22:28 28-01-08

>>>> to get all 1s, better is ~0
>>> How is that better?
>> should work on 1s-complement machines.

CBFalconer <c...@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.

Re: Simple C question...entering binary - Eric Smith - 22:31 28-01-08

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.

previous | 1 | 2 | 3 | 4 | 5 | next