Get Nth bit value from a 32-bit number 
Author Message
 Get Nth bit value from a 32-bit number

There is something wrong with my damn news server, so if this has been
posted multi times, i am sorry!!

I need to retrieve a value from different bytes from a 32-bit integer.
lets say I have to following code:

int A = 0;

A = FuncThatSetsA();

//end

now I need to lets say, get the value (1 or 0) from the 24th bit, or
lets say I need the value of the last bit, bit number 31 (saying we
number the bits 0 thru 31 how would I do that.

What I am trying to do, is I have a message I getting from windows,
and the lParam is like so:
0 -> 15: Key Repeate Count
16-> 23: Scan Code
24:     Exteneded Key Flag
25->28: Reserved
29 Context Code
30: Prev Key State
31: Transition State Flag

WHat I need to do is really find out if it is a keyup or keydown
message, and ignore all others.
so what I need to do is get the value of 31, if it is set to 0 it is
keyup, if it is set to 1, it is keydown.

I was browsing thru MSDN, and I came on a page and it says I can use
the KF_* const's to manipulate the keystroke flags, so what I thought
they meant was something like:

int iState = 0;
int A = 0;
A = FuncThatSetsA();
iState = (KF_UP & A);
if (iState == 0)
{
//KeyUp

Quote:
}

else
{
//KeyDown

Quote:
}

Well, any help would be GREAT!!!!

Thanx,
Donkey

DOS never says "EXCELLENT command or filename"



Mon, 30 Jul 2001 03:00:00 GMT  
 Get Nth bit value from a 32-bit number
Hi Donkey,

Your close.  The ampersand is masking the bits however the result isn't what
your thinking it is.

iState = (KF_UP & A);

iState now contains the result of the mask which will be either:

0x00000000
or
0x80000000

assuming KF_UP is defined as 0x80000000.  So you need to either:

iState = (KF_UP & A) == KF_UP;

or

if ( iState == KF_UP )

HTH
--
=================
Frank Hickman
NobleSoft, Inc.


=================



Mon, 30 Jul 2001 03:00:00 GMT  
 Get Nth bit value from a 32-bit number
I need to retrieve a value from different bytes from a 32-bit integer.
lets say I have to following code:

int A = 0;

A = FuncThatSetsA();

//end

now I need to lets say, get the value (1 or 0) from the 24th bit, or
lets say I need the value of the last bit, bit number 31 (saying we
number the bits 0 thru 31 how would I do that.

What I am trying to do, is I have a message I getting from windows,
and the lParam is like so:
0 -> 15: Key Repeate Count
16-> 23: Scan Code
24:     Exteneded Key Flag
25->28: Reserved
29 Context Code
30: Prev Key State
31: Transition State Flag

WHat I need to do is really find out if it is a keyup or keydown
message, and ignore all others.
so what I need to do is get the value of 31, if it is set to 0 it is
keyup, if it is set to 1, it is keydown.

I was browsing thru MSDN, and I came on a page and it says I can use
the KF_* const's to manipulate the keystroke flags, so what I thought
they meant was something like:

int iState = 0;
int A = 0;
A = FuncThatSetsA();
iState = (KF_UP & A);
if (iState == 0)
{
//KeyUp

Quote:
}

else
{
//KeyDown

Quote:
}

Well, any help would be GREAT!!!!

Thanx,
Donkey

DOS never says "EXCELLENT command or filename"



Mon, 30 Jul 2001 03:00:00 GMT  
 Get Nth bit value from a 32-bit number
I need to retrieve a value from different bytes from a 32-bit integer.
lets say I have to following code:

int A = 0;

A = FuncThatSetsA();

//end

now I need to lets say, get the value (1 or 0) from the 24th bit, or
lets say I need the value of the last bit, bit number 31 (saying we
number the bits 0 thru 31 how would I do that.

What I am trying to do, is I have a message I getting from windows,
and the lParam is like so:
0 -> 15: Key Repeate Count
16-> 23: Scan Code
24:     Exteneded Key Flag
25->28: Reserved
29 Context Code
30: Prev Key State
31: Transition State Flag

WHat I need to do is really find out if it is a keyup or keydown
message, and ignore all others.
so what I need to do is get the value of 31, if it is set to 0 it is
keyup, if it is set to 1, it is keydown.

I was browsing thru MSDN, and I came on a page and it says I can use
the KF_* const's to manipulate the keystroke flags, so what I thought
they meant was something like:

int iState = 0;
int A = 0;
A = FuncThatSetsA();
iState = (KF_UP & A);
if (iState == 0)
{
//KeyUp

Quote:
}

else
{
//KeyDown

Quote:
}

Well, any help would be GREAT!!!!

Thanx,
Donkey

DOS never says "EXCELLENT command or filename"



Mon, 30 Jul 2001 03:00:00 GMT  
 Get Nth bit value from a 32-bit number
I need to retrieve a value from different bytes from a 32-bit integer.
lets say I have to following code:

int A = 0;

A = FuncThatSetsA();

//end

now I need to lets say, get the value (1 or 0) from the 24th bit, or
lets say I need the value of the last bit, bit number 31 (saying we
number the bits 0 thru 31 how would I do that.

What I am trying to do, is I have a message I getting from windows,
and the lParam is like so:
0 -> 15: Key Repeate Count
16-> 23: Scan Code
24:     Exteneded Key Flag
25->28: Reserved
29 Context Code
30: Prev Key State
31: Transition State Flag

WHat I need to do is really find out if it is a keyup or keydown
message, and ignore all others.
so what I need to do is get the value of 31, if it is set to 0 it is
keyup, if it is set to 1, it is keydown.

I was browsing thru MSDN, and I came on a page and it says I can use
the KF_* const's to manipulate the keystroke flags, so what I thought
they meant was something like:

int iState = 0;
int A = 0;
A = FuncThatSetsA();
iState = (KF_UP & A);
if (iState == 0)
{
//KeyUp

Quote:
}

else
{
//KeyDown

Quote:
}

Well, any help would be GREAT!!!!

Thanx,
Donkey

DOS never says "EXCELLENT command or filename"



Mon, 30 Jul 2001 03:00:00 GMT  
 Get Nth bit value from a 32-bit number


Quote:
>Hi Donkey,

>Your close.  The ampersand is masking the bits however the result isn't what
>your thinking it is.

>iState = (KF_UP & A);

>iState now contains the result of the mask which will be either:

>0x00000000
>or
>0x80000000

>assuming KF_UP is defined as 0x80000000.  So you need to either:

>iState = (KF_UP & A) == KF_UP;

>or

>if ( iState == KF_UP )

>HTH

Ok, if I do it that way, lets say I don't have a pre-defined mask, and
I have to figure it out myself.  Lets say I need bits 16 thru 23, if I
convert the binary number:

00000000000000001111111100000000,
it converts to:
0xFF00

but if I use that mask to do:

int R;
R = (0xFF00 & lParam);

it does not give the right results.
Can you explain if I am way off base by getting my masks this way?
I don't even know if I am on the right track or not :(

Donkey

DOS never says "EXCELLENT command or filename"



Tue, 31 Jul 2001 03:00:00 GMT  
 Get Nth bit value from a 32-bit number
Hi Donkey,

In 32-bit applications "int" is four bytes not two so to test bits 16 through 23
use the mask (assuming zero base 0-31):

0x00FF0000

HTH
--
=================
Frank Hickman
NobleSoft, Inc.


=================



Wed, 01 Aug 2001 03:00:00 GMT  
 Get Nth bit value from a 32-bit number

Quote:

>I need to retrieve a value from different bytes from a 32-bit integer.
>lets say I have to following code:

>int A = 0;

>A = FuncThatSetsA();

>//end

>now I need to lets say, get the value (1 or 0) from the 24th bit, or
>lets say I need the value of the last bit, bit number 31 (saying we
>number the bits 0 thru 31 how would I do that.

I like to number my bits starting with 31 so the bit number is the power of
2 the bit corresponds to.

Anyway, what I would do is create a bit mask in which the bit you want to
test is set.  Supposing you want to test bit 24 using the numbering scheme I
mentioned above, do

const DWORD Bit24 = 1<<24;

You can then test your integer A using the bitwise operator & as follows.

if (A & Bit24) { whatever; }



Fri, 03 Aug 2001 03:00:00 GMT  
 Get Nth bit value from a 32-bit number
The general solution is "value >> bit_number & 1", which will always
give you 1 or 0.  However, if "bit_number" is a constant, it might be
slightly more efficient to do "value & 1 << bit_number" (which will
return either 1 << bit_number or 0), because the compiler can calculate
"1 << bit_number" at compilation time.
Quote:

> I need to retrieve a value from different bytes from a 32-bit integer.
> lets say I have to following code:

> int A = 0;

> A = FuncThatSetsA();

> //end

> now I need to lets say, get the value (1 or 0) from the 24th bit, or
> lets say I need the value of the last bit, bit number 31 (saying we
> number the bits 0 thru 31 how would I do that.



Fri, 03 Aug 2001 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. masking 1,2,4,8,16 bit values from 32 bit int

2. how to write and read two 16 bit values into one 32 bit

3. 32 bit Icons and C# VS.net (24 bit + 8 bit alpha)

4. Getting date and time from two 32 bit numbers

5. 32-bit VC 2.2 DLLs with 32-bit VB4

6. top 32 bits of 64-bit product of two 32-bit integers

7. Linking 32 bit VC++4.2 DLLs with VB4.0 32 bit

8. Loading a 32-Bit DLL into a 16-Bit DLL

9. Help: porting 32-bit app to 64-bit Dec Alpha

10. 16 bit Library to a 32 bit library ...

11. Casting a 32-bit unsigned integer to a 16-bit unsigned integer

12. 16 bit code to 32 bit

 

 
Powered by phpBB® Forum Software