| Arduino Forum ::  Members :: jraskell
 Show Posts Pages: 1 ... 50 51 [52] 53
 766 Forum 2005-2010 (read only) / Syntax & Programs / Re: Urgent - Split Long into 2 Int using BitShift on: January 20, 2011, 03:22:11 pm The following should work to convert the two ints back into a long:newlong = ((long)H << 16) | L;But it will depend on the platform and programming environment you are working in to conver it back.  This assumes that H and L are two 16 bit variables, and that (long) is a 32 bit variable.  On most modern PCs and programming languages, ints are 32 bit variables and longs are 64 bit variables.  H and L may need to be declared as shorts and your 32 bit variable just declared as an int. (and the cast would be (int) instead of (long) as well)
 767 Forum 2005-2010 (read only) / Syntax & Programs / Re: Urgent - Split Long into 2 Int using BitShift on: January 20, 2011, 01:15:03 pm I'm not quite sure what your expectations are here.  The intermediate int decimal values will in no way correlate to the original long decimal value.  If they are only being used as a necessary step to transfer the entire data, then that doesn't matter.What matters is that the bit representation is maintained.Say you have a long value of (just for example) 1,582,081.It's binary representation would be:00000000 00011000 00100100 00000001If you then assign that value to two ints with bitwise shifting, you would end up withhigh int 00000000 00011000 low  int 00100100 00000001If you then properly recompose that back into a new long, that new long will have the same decimal value as the previous one (assuming the binary representation of that decimal value is the same across any platform transitions).You could use unsigned ints for your intermediate variables and it wouldn't make a difference.  You could even use 4 bytes or chars instead.QuoteWith -1 as ValueLong I read on serial monitor:L = 11111111111111111111111111111111H = 11111111111111111111111111111111That looks completely correct.  Signed ints are stored in a twos complement format.  Google it for more info on how twos complement works, and why it's an ideal format to use for signed ints.
 768 Forum 2005-2010 (read only) / Syntax & Programs / Re: Urgent - Split Long into 2 Int using BitShift on: January 20, 2011, 11:44:22 am Bitwise manipulation in general doesn't care one bit (no pun intended) about the sign or format of the variable they are operating on.If you decompose a 4 byte variable into two 2 byte variables, and then later recompose them back into a 4 byte variable, the bit patterns will remain identical.That's not to say there won't be any sign issues, but they will not be related in any way to the bitwise operations (for example, there may be endian problems if you are transferring the data between platforms with different endian'ness').
 769 Forum 2005-2010 (read only) / Syntax & Programs / Re: Urgent - Split Long into 2 Int using BitShift on: January 20, 2011, 11:28:21 am Just use:HighINT = ValueLong >> 16;LowINT  = ValueLong;And be done with it.  The masking is unnecessary.  The results won't match your other method, but that is because, as AWOL has correctly pointed out, your other method is incorrect.
 770 Forum 2005-2010 (read only) / Syntax & Programs / Re: -output value to +output value? on: January 20, 2011, 04:16:56 pm What you have is almost what you need.  Just needs two tweaks...change +output to -output in your second analogWrite.One of your if statements should handle the case where outputValue = 0.  Either use >= in the first line, or <= in the second line.