# Test_bit On Integer?

## Recommended Posts

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

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

Why don't you try some thing like, if( var.8==0) or say if(var.15!=1).

Raghunathan.

##### Share on other sites

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)
...```

```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 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 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 on other sites

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

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

Sorry what do you mean? I am not that experienced with C/C++

##### 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 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 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 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 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.

## 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.

×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

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