LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

help with bit operations

I am sure that this is an easy trivial thing but I cant get it, and all my programming co-workers are off this week so no one to ask but here.

I want to be able to retrieve any particular bit of a 16 bit integer. I need to use these as integer values in my CVI program.

Seems like there is a bit operator that will do this but I cant for the life of me remember it and the searches I am doing are coming up empty. Or are there any functions in CVI library that will do this?
0 Kudos
Message 1 of 8
(4,490 Views)
You must use the '&' operator in conjunction with a proper mask, as per the following example (assuming 'dgt' is the integer you wanto to decode):

bit1 = dgt & 0x01;
bit2 = dgt & 0x02;
bit3 = dgt & 0x04;
bit4 = dgt & 0x08;
bit5 = dgt & 0x10;
bit6 = dgt & 0x20;
bit7 = dgt & 0x40;
bit8 = dgt & 0x80;

Roberto


Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 8
(4,490 Views)
OK this jogs my memory. AND the bit with 1 to get the value of the bit, 1 & 1=1, 1 & 0=0.

But when I put this into CVI

bit3 = 15 & 0x04;

after the line is run, bit3 is 4. I need to have a 1 or 0 answer corresponding to whether the bit I want to check is a 0 or a 1.
0 Kudos
Message 3 of 8
(4,490 Views)
Below is a code snippet that gives an example. It's crude but I tested it
using CVI 6.0 and it worked.

Hope it helps,
George Hodgson

// 'value' contains the bits of interest
// bits[] is a 16 element array where the result is stored
// On exit each element of 'bits[]' will contain either a 1 or 0
// to indicate the state of the first 16 bits in the integer 'value'.
//
// 'bits[0]' corresponds to bit position 0 up through
// 'bits[15]' which corresponds to bit position 15

void GetBits(void)
{
int value = 0xa0a0; // arbitrary value for illustrative purposes
int bits[16]; // 1 = bit set, 0 = bit clear
// the two values above would normally be declared elsewhere
int bitposition = 1; // start at bit position 0
int counter = 0; // loop counter

while (counter <
16) {
if (value & bitposition) // bitwise AND operation
bits[counter] = 1; // bit was set
else
bits[counter] = 0; // bit was zero
bitposition = bitposition << 1; // bitwise SHIFT LEFT operation
counter++;
}
}

"kmagas" wrote in message
news:50650000000800000039E20000-1079395200000@exchange.ni.com...
> I am sure that this is an easy trivial thing but I cant get it, and
> all my programming co-workers are off this week so no one to ask but
> here.
>
> I want to be able to retrieve any particular bit of a 16 bit integer.
> I need to use these as integer values in my CVI program.
>
> Seems like there is a bit operator that will do this but I cant for
> the life of me remember it and the searches I am doing are coming up
> empty. Or are there any functions in CVI library that will do this?
Message 4 of 8
(4,490 Views)
Bitfields and structures have not been invented for this problem, but a nice
and dirty solution is to use them:

try this function for 8 bits, I leave it to you to exapnd it to any other
built in type



int BitfieldTest (char c, int bit){
int status;
typedef struct{
unsigned char bit0:1;
unsigned char bit1:1;
unsigned char bit2:1;
unsigned char bit3:1;
unsigned char bit4:1;
unsigned char bit5:1;
unsigned char bit6:1;
unsigned char bit7:1;

}FLAG;

union {
char c;
FLAG f;

}BF;

BF.c=c;
switch(bit){
case 0:status=(int)BF.f.bit0; break;
case 1:status=(int)BF.f.bit1; break;
case 2:status=(int)BF.f.bit2; break;
case 3:status=(int)BF.f.bit3; break;
case 4:status=(int)BF.f.bit4; break;
case 5:status=(int)BF
.f.bit5; break;
case 6:status=(int)BF.f.bit6; break;
case 7:status=(int)BF.f.bit7; break;
default: status=-1;
}

return status;
}


"kmagas" wrote in message
news:50650000000800000039E20000-1079395200000@exchange.ni.com...
> I am sure that this is an easy trivial thing but I cant get it, and
> all my programming co-workers are off this week so no one to ask but
> here.
>
> I want to be able to retrieve any particular bit of a 16 bit integer.
> I need to use these as integer values in my CVI program.
>
> Seems like there is a bit operator that will do this but I cant for
> the life of me remember it and the searches I am doing are coming up
> empty. Or are there any functions in CVI library that will do this?
0 Kudos
Message 5 of 8
(4,490 Views)
kmagas wrote in message news:<50650000000500000097A20100-1079395200000@exchange.ni.com>...
> OK this jogs my memory. AND the bit with 1 to get the value of the
> bit, 1 & 1=1, 1 & 0=0.
>
> But when I put this into CVI
>
> bit3 = 15 & 0x04;
>
> after the line is run, bit3 is 4. I need to have a 1 or 0 answer
> corresponding to whether the bit I want to check is a 0 or a 1.

I suggest you to search comp.lang.c newsgroup, where I found the following function:

//-------------------------------
int TestBit ( int Value, int Bit )
{
return ( Value & ( 1 << Bit ) );
}
//-------------------------------

Elio
0 Kudos
Message 6 of 8
(4,490 Views)
Just to throw one more in the mix:

unsigned short value,bitposition,counter;
char bits[16];

value = 0xaaaa;
for (bitposition=1,counter=0;counter<16;counter++,bitposition<<=1)
bits[counter]=(value&bitposition)?1:0;

"kmagas" wrote in message
news:50650000000800000039E20000-1079395200000@exchange.ni.com...
> I am sure that this is an easy trivial thing but I cant get it, and
> all my programming co-workers are off this week so no one to ask but
> here.
>
> I want to be able to retrieve any particular bit of a 16 bit integer.
> I need to use these as integer values in my CVI program.
>
> Seems like there is a bit operator that will do this but I cant for
> the life of me remember it and the searches I am doing are coming up
> empty. Or are there a
ny functions in CVI library that will do this?
0 Kudos
Message 7 of 8
(4,490 Views)
Thanks for all the help. I ended up using George Hodgson's basic function, modifying it slightly to allow an integer value and the bit requested to be passed in.

int get_bit_value(int value,int desired_bit)
{
int bits[16]; // 1 = bit set, 0 = bit clear
int bitposition = 1; // start at bit position 0
int counter = 0; // loop counter
int r;

while (counter < 16)
{
if (value & bitposition) // bitwise AND operation
bits[counter] = 1; // bit was set
else
bits[counter] = 0; // bit was zero
bitposition = bitposition << 1; // bitwise SHIFT LEFT operation
counter++;
}
r=bits[desired_bit];
return r;
}
0 Kudos
Message 8 of 8
(4,490 Views)