| Author |
Message |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 encoding an integer into a bit array (binary number)
hi to all,
I'm looking for a fast and simple code to encode an integer number into a bit array (binary number).
The code I wrote and that I currently use is the following:
is there any a simpler and faster one?
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Tue Jul 29, 2008 5:48 pm |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
 Re: encoding an integer into a bit array (binary number)
hmmh: maybe this...:
any other proposals?
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Wed Jul 30, 2008 6:47 am |
|
 |
|
AvidProgrammer
Rookie
Joined: Wed Jun 25, 2008 6:07 pm Posts: 46
|
Ford,
I haven't had a chance to try this with the actual RobotC compiler, but this code should work for you. (It worked fine on the compiler I just tried it with.) Using it would cut out the comparison you are doing.
|
| Wed Jul 30, 2008 12:12 pm |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
hi,
great, really great, it's working perfect!
I just changed the for-loop counter from decreasing to increasing
so now it's even working in the same "direction" as my own code.
And I think your's must be much faster .
Thx a lot!
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
Last edited by Ford Prefect on Wed Jul 30, 2008 4:04 pm, edited 1 time in total.
|
| Wed Jul 30, 2008 12:45 pm |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
PS:
what exactly does
i >>= 1;
do?
I know >> 1
and I know =1
but >>=1 
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Wed Jul 30, 2008 3:58 pm |
|
 |
|
AvidProgrammer
Rookie
Joined: Wed Jun 25, 2008 6:07 pm Posts: 46
|
Ford,
'>>=' is a operation to shift, then assign.
Just like '+=' will add, then assign the result to the variable, '>>=' will shift by the specified number of bits, then assign the result back to the variable on the left.
So, 'i >>= 1' tells the compiler to shift i's bits to the right by one, then store that result back in i.
In essence, what the code is doing is ripping a single bit at a time off the variable you pass in. Inside the loop, we set your array variable to the value of the lowest bit, then shift that bit away and repeat with the next bit, which is now in the least-significant bit spot.
|
| Wed Jul 30, 2008 4:25 pm |
|
 |
|
Ford Prefect
Senior Roboticist
Joined: Sat Mar 01, 2008 12:52 pm Posts: 936 Location: a small planet in the vicinity of Beteigeuze
|
aaaah...!
thx a lot! 
_________________ Ford Prefect
Never purchase release 1.x ! (ancient programmer's wisdom) "Don't argue with idiots. They'll drag you down to their level and then beat you with experience."
|
| Thu Jul 31, 2008 2:16 am |
|
|