![]() |
Manipulating bits
Ok, I'm having a bit of trouble. I have been experimenting with using an off-board and so far everything has been going great. But now I'm having trouble fitting 10 gallons in an 8 gallon hat. My problem is that we have set up the serial link to send 8 bits at a time. Its perfect for sending joystick or pwm values back and forth but what happens when I want to send an integer that I have given an analog value? I know I can bit shift off the two right most bits and then replace them when they get to the coprocessor with zeros. That gives me the same value as when it was sent within 3. My question is, is there a way in c to take the two right-most bits of a value and put them into a byte and then fill up the rest of the byte with other right most bits of values so I don't wast space. I know how to cut this byte back up and distribute the bits to their corresponding values (my coprocessor runs on python) but I don't know how I would package and send something like this in C. Does anyone have a solution to this problem?
-Don |
Re: Manipulating bits
Read up on the bitwise operators:
~ (NOT) & (AND) ^ (XOR) | (OR) You can use these to manipulate bits pretty easily. |
Re: Manipulating bits
OK, got it. There was a great example on wikipedia.
Thanks! -Don |
Re: Manipulating bits
It looks like you are trying to "serialize" or "marshal" your data. Googling these terms may help.
In C, one nifty trick is to recast your data as strings. Code:
int n=4; |
Re: Manipulating bits
One word of warning about the PIC, though it probably doesn't apply in your case if you're just using shifts for bit manipulation. Right shifts are unsigned only. So if you have a signed int that you want to divide by 4, x >> 2 only works if the value is positive. A negative value will have 0 shifted into the high order bits and will end up looking like a positive number.
|
Re: Manipulating bits
Code:
144: var1 = var1 >> 2;If you want to do signed shifts, you have to roll your own and change the instructions to: Code:
BTFSC 0xe7, 7, BANKEDCode:
if (var1 > 0) |
| All times are GMT -5. The time now is 07:13. |
Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright © Chief Delphi