[Emc-developers] Right-justifying

2011-03-31 Thread andy pugh
I am trying to put a signed 16 bit number into the rightmost 16 bits of a u32 buffer. I have something that nearly works: test = (analogue_out(0) < 0 ) ? 0x8000 | (u32)(analogue_out(0) * scale(0) * -3276.7) : 0x | (u32)(analogue_out(0) * scale(0) * 3276.7); (Though, unexpec

Re: [Emc-developers] Right-justifying

2011-03-31 Thread Pavel Shramov
On Fri, Apr 01, 2011 at 01:13:54AM +0100, andy pugh wrote: > I am trying to put a signed 16 bit number into the rightmost 16 bits > of a u32 buffer. > However, surely there must be a tidier and faster way? (probably using > s16 typecasting and a bitwise OR? Yes, there is. u32_var = ((u32) (u16) s

Re: [Emc-developers] Right-justifying

2011-04-01 Thread andy pugh
On 1 April 2011 06:02, Pavel Shramov wrote: > u32_var = ((u32) (u16) s16_var); > > u16 casts s16 to 16 bit positive number. So, (s16)-10 == 32778? (I could just check, but I won't be near a C compiler until sunday) -- atp "Torque wrenches are for the obedience of fools and the guidance of wis

Re: [Emc-developers] Right-justifying

2011-04-01 Thread Pavel Shramov
On Fri, Apr 01, 2011 at 09:34:20AM +0100, andy pugh wrote: > On 1 April 2011 06:02, Pavel Shramov wrote: > > > u32_var = ((u32) (u16) s16_var); > > > > u16 casts s16 to 16 bit positive number. > > So, (s16)-10 == 32778? (s16) -10 == -10 (u16) -10 == 0xfff6 You may check it with shell: $ printf

Re: [Emc-developers] Right-justifying

2011-04-01 Thread Brian
How about a union? I don't know the exact syntax off hand, but I know it will do exactly what you are trying. Brian On Fri, Apr 1, 2011 at 4:57 AM, Pavel Shramov wrote: > On Fri, Apr 01, 2011 at 09:34:20AM +0100, andy pugh wrote: >> On 1 April 2011 06:02, Pavel Shramov wrote: >> >> > u32_var =

Re: [Emc-developers] Right-justifying

2011-04-01 Thread Pavel Shramov
On Fri, Apr 01, 2011 at 09:35:35PM -0400, Brian wrote: > How about a union? I don't know the exact syntax off hand, but I know > it will do exactly what you are trying. By the way great idea - it'll simplify code for futher authors. But union is not needed here - struct is sufficient. Something l