View unanswered posts | View active topics It is currently Wed Apr 25, 2018 9:18 pm

 Page 1 of 1 [ 7 posts ]
 Print view Previous topic | Next topic
encoding an integer into a bit array (binary number)
Author Message
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
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:
 Code:int BitArray[nbin];  // e.g.nbin=16 for max. Integer= (2^16)-1=32767 void SetBitPattern(int i) {   int n;     for (n=0; n

is there any a simpler and faster one?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Tue Jul 29, 2008 5:48 pm
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Re: encoding an integer into a bit array (binary number)
hmmh: maybe this...:
 Code:int BitArray[nbin];  // e.g.nbin=16 for max. Integer= (2^16)-1=32767 void SetBitPattern(int i) {   int n;     for (n=0; n0)?1:0) ;   } }

any other proposals?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Wed Jul 30, 2008 6:47 am
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.

 Code:const int nbin = sizeof(int) * 8;int BitArray[nbin];void SetBitPattern(int i){  int n;  for (n = nbin - 1; n >= 0; n--)  {    BitArray[n]= i & 1;    i >>= 1;  }}

Wed Jul 30, 2008 12:12 pm
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030

hi,
great, really great, it's working perfect!
I just changed the for-loop counter from decreasing to increasing
 Code:for (n = 0; n <=ni-1; n++)

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!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

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
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030

PS:
what exactly does
i >>= 1;
do?
I know >> 1
and I know =1
but >>=1

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Wed Jul 30, 2008 3:58 pm
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
Guru

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030

aaaah...!
thx a lot!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)

Thu Jul 31, 2008 2:16 am
Display posts from previous:  Sort by
 Page 1 of 1 [ 7 posts ]

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ ROBOTC Applications    ROBOTC for LEGO MINDSTORMS       Third-party sensors    ROBOTC for CORTEX & PIC    ROBOTC for VEX IQ    ROBOTC for Arduino    Robot Virtual Worlds    Multi-Robot Communications    Issues and Bugs Competitions & Partners    Mini Urban Challenge    CS2N Robot Virtual Worlds Competitions       VEX Skyrise Competition 2014-2015       VEX Toss Up 2013-2014       FTC Block Party! 2013-2014    Competitions using VEX - BEST, TSA, VEX, and RoboFest!    FTC Programming    RoboCup Junior and Other ROBOT Competitions Virtual Brick Robotics Discussions    General Discussions    Project Discussions Off-Topic ROBOTC Forum & ROBOTC.net Suggestions/Feedback    ROBOTC Forums Suggestions/Comments    ROBOTC.net Suggestions/Comments       NXT Programming: Tips for Beginning with ROBOTC       VEX Programming: Tips for Beginning with ROBOTC    2013 Robotics Summer Of Learning       VEX Toss Up Programming Challenge       FTC Ring It Up! Programming Challenge    International Forums       Spanish Forums          ROBOTC for MINDSTORMS          ROBOTC for VEX       French Forums          ROBOTC pour Mindstorms          ROBOTC pour IFI VEX       Japanese Forums （日本語のフォーラム）       German Forums    2015 Spring Carnival Event    PLTW (Project Lead The Way)    Robotics Merit Badge    2014 Robotics Academy Summer of Learning