Working with union and struct bit fields

Source: Internet
Author: User

For a long time the union and struct bit fields in C were not manipulated.

Recently Used (I admit, of course, that I copied it from StackOverflow).

The requirement is this, known as a LPARAM integer 3866625, to find out what the value of the 30th bit is.

The solution is quite simple.

int 3866625  = a>>0x0001; Note that it is right to move 30, not 29, because the first bit is 0, and I started moving the 29 result wrong!

But I accidentally saw another scenario on stack over flow.

Union keystate{LPARAM LPARAM; struct{unsigned nrepeatcount: -; Unsigned nscancode:8; Unsigned nextended:1; Unsigned nreserved:4; Unsigned ncontext:1; Unsigned nprev:1; Unsigned Ntrans:1; };}; KeyState Ks;ks.lparam=3866625; ks.nprev; This is 30th place .


And then we checked. Union,union is characterized by shared memory and the same initial address. The length is calculated with the maximum member length.

Next is the bit-field operator for the struct.

struct

{

unsigned a:1;

unsigned b:2;

}

Represents a member of 1 bytes, and a member of B is 2 bytes.

The effect of combining 2 of them is

When you access the nprev field of union, you are actually accessing the bit after the 16+8+1+4+1 digit. and a value of length 1. Comparing the start-up operation, although a little longer, but very easy to understand

What's even better is that you can access any of the other bits.

This bit operation is mainly used to scan the message with the computational keyboard.

Keystroke Message Flags

The lParam parameter of a keystroke message contains additional information about the keystroke that generated th E message. This information includes the repeat count, the scan code, the EXTENDED-KEY flag, the context code, the previous Key-state Flag, and the TRANSITION-STATE flag. The following illustration shows the locations of these flags and values in the lParam parameter.

A application can use the following values to manipulate the keystroke flags.

Kf_altdown Manipulates the ALT key flag, which indicated if the ALT key is pressed.
Kf_dlgmode Manipulates the dialog mode flag, which indicates whether a dialog box is active.
kf_extended Manipulates the extended key flag.
Kf_menumode Manipulates the menu mode flag, which indicates whether a menu is active.
Kf_repeat Manipulates the repeat count.
Kf_up Manipulates the transition state flag.

Working with union and struct bit fields

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.