|
-
September 26th, 1999, 05:59 AM
#1
Bits
Hi all,
I have a list like this and would like to know which bits they are:
0x1 = Bit ?
0x2 = Bit ?
0x4
up to
0x20000000
I have a total of 29 values. How can I calcutalte which bit each value is?
Can anyone help?
Thanks.
Martin
-
September 27th, 1999, 05:44 AM
#2
Re: Bits
Hi
Try something like this
int WhichBit( long value )
{
long mask = 1 ;
for ( int i = 0 ; i < 32 ; i++ )
{
if ( ( mask & value ) != 0 )
{
return i+1 ;
}
mask <<= 1 ;
}
}
Hope this helps
Gabriel
-
September 27th, 1999, 09:56 AM
#3
Re: Bits
what is this :
mark <<= 1 ???
-
September 27th, 1999, 10:30 AM
#4
Re: Bits
it is the same as
mask = mask << 1
operator << shifts bits to the left
-
September 28th, 1999, 09:30 AM
#5
Re: Bits
That presumes that value only has one bit set. (If it has more than one bit set, it will only find the least significant). If you are going to use that assumption, we can simplify it a bit more:
int WhichBit(long value)
{
int bit = 0;
while (value != 0)
{
value >>= 1;
bit++;
}
return (bit);
}
This returns zero if no bit is set, or the position (1-32) of the most significant bit set.
Truth,
James
http://www.NJTheater.com
http://www.NJTheater.com/JamesCurran
-
September 28th, 1999, 09:58 AM
#6
Re: Bits
Hi,
I hope it is in powers of 2.
int GetBitPos(long value)
{
long mask = 1;
int ct = 0;
while(ct < 32)
{
if ( value & mask )
{
return ct;
}
value >>= 1;
ct++;
}
return -1;
}
Hope this helps
Viswanathan
-
September 29th, 1999, 10:09 AM
#7
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|