View unanswered posts | View active topics It is currently Thu Oct 23, 2014 10:00 am






Reply to topic  [ 7 posts ] 
encoding an integer into a bit array (binary number) 
Author Message
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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<nbin; n++)
  {
    BitArray[n]= (i & (1<<n)) / (1<<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)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Tue Jul 29, 2008 5:48 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 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; n<nbin; n++)
  {
    BitArray[n]= (((i & (1<<n))>0)?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)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Wed Jul 30, 2008 6:47 am
Profile
Rookie

Joined: Wed Jun 25, 2008 6:07 pm
Posts: 46
Post 
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
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
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)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


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
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
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)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Wed Jul 30, 2008 3:58 pm
Profile
Rookie

Joined: Wed Jun 25, 2008 6:07 pm
Posts: 46
Post 
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
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post 
aaaah...!
thx a lot! :idea:

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Thu Jul 31, 2008 2:16 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 7 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


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

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.