Jump to content
Sign in to follow this  
sdujolo

Test_bit On Integer?

Recommended Posts

Raghunathan's suggestion will work in most cases. If you want to test a bit in a value like my_array[3] or 15 (anything but a normal variable), though, it won't work, even if you use parentheses. Then you have two options that I've seen:

1) create a temporary variable to hold the value and hope it optimizes out properly

int tmp = my_array[3];
if (tmp.5)
  ...

2) use bit masks

if ((my_array[3] & 0x20) == 0)
  ...

Keep in mind the second solution will return an integer and the value won't necessarily be 1 or 0. Also watch precedence, since (val & 0x04 == 0) simplifies to (val & false).

Share this post


Link to post
Share on other sites

Thanks for your answer I ended up in split it in two char see code below. Any better solution?

 

void send_nexa_cmd(char cmd1, char cmd2)
{
char i,j;
for (j = 0; j < 2; j++ ) {
	for (i = 0; i < 8; i++) {
		if ( test_bit(cmd1, i) ) {
			send_one();
		}else {
			send_zero();
		}
	}
	for (i = 0; i < 4; i++) {
		if ( test_bit(cmd2, i) ) {
			send_one();
		}else {
			send_zero();
		}
	}
	send_stop();
	delay_ms(10);
}
Printf("OK\n\r");
}

Share this post


Link to post
Share on other sites
Thanks for your answer I ended up in split it in two char see code below. Any better solution?

I think as long as you got something that works, "better" is a matter of opinion and depends on your situation, but here's how I would do it:

void send_nexa_cmd(char cmd)
{
   char i,j;
   for (j = 0; j < 2; j++ ) {
       for (i = 0; i < 12; i++) {
           if ( (cmd >> i) & 1 ) {
               send_one();
           }else {
               send_zero();
           }
       }
       send_stop();
       delay_ms(10);
   }
   Printf("OK\n\r");
}

Edited by edt

Share this post


Link to post
Share on other sites

i am wondering why you dont just use a bit mask and compare instead of all this fiddling ...

Share this post


Link to post
Share on other sites
i am wondering why you dont just use a bit mask and compare instead of all this fiddling ...

I'm not quite sure what you mean, either. If you're talking specifically about the way I suggested, I didn't want to make a new integer temp variable for the mask, so I used a constant mask (0x01) and shifted the data down to it instead of shifting the mask up. I'm sure there are ways that optimize better, but I was aiming to be concise.

Share this post


Link to post
Share on other sites
i am wondering why you dont just use a bit mask and compare instead of all this fiddling ...

I'm not quite sure what you mean, either. If you're talking specifically about the way I suggested, I didn't want to make a new integer temp variable for the mask, so I used a constant mask (0x01) and shifted the data down to it instead of shifting the mask up. I'm sure there are ways that optimize better, but I was aiming to be concise.

 

Yes, kinda like the way you did it. ... i had to read it a few times before i figured

out that is what you were doing ... different people = different ways for the same thing

Share this post


Link to post
Share on other sites

As a side note, I believe SourceBoost supports bits as function parameters (but not as return types, etc.), so instead of send_one(void) and send_zero(void), you might get better mileage with send_bit(bit x). You'll probably avoid repeating yourself more that way.

Share this post


Link to post
Share on other sites
How to test a bit on a integer value? test_bit(var, num) is only works up to 8bit

 

test_bit(var, num) is a macro, so I don't see why it shouldn't be possible to test bits in words above 8 bits, provided you define var as an integer.

 

This code should make high portc.0 and portc.3

 

#include <system.h>

#pragma DATA 0x2007, _HS_OSC & _WDT_OFF

void main()
{ unsigned int word;
  
  trisc = 0;
  portc = 0;
  
  word = 0b0110111000111;
     
  if (test_bit(word, 11)) // this uses test_bit macro
     portc.0 = 1;         // true
  else
     portc.1 = 1;
     
  // this is the same implementation of test_bit macro. 
  if (word & (1 << 9))
     portc.2 = 1;
  else
     portc.3 = 1;   // true       
  
  while(1);
}

Share this post


Link to post
Share on other sites
As a side note, I believe SourceBoost supports bits as function parameters (but not as return types, etc.), so instead of send_one(void) and send_zero(void), you might get better mileage with send_bit(bit x).  You'll probably avoid repeating yourself more that way.

 

Thanks for your answers :unsure:

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...