C language in the bottom of the operation of C language

Source: Internet
Author: User
Tags bitwise bitwise operators

Overview

C Language memory model basically corresponds to now von Neumann (Neumann) the actual storage model of the computer, very good to achieve the mapping of the machine, which is the main reason for C/C + + suitable for the bottom development, in addition, there is another reason for the base development That is the C language to the bottom of the operation to do a lot of support, provides a lot of relatively low-level features.

The following questions are discussed separately.

Problem: Shift operation

When using the shift operator, there are two issues to be clear:

(1), in the right shift operation, the vacated position is to fill in 0 or sign bit;

(2), what number can be used to shift the number of digits.

Answer and Analysis:

">>" and "<<" refers to moving each digit in a variable to the right or left, usually in the form of:

Move right: Variable name >> number of bits shifted

Move left: Variable name << number of bits shifted

After displacement, the bits of one end are "squeezed out," while the vacated bits on the other end are filled with 0, and the shift in the C language is not circular.

(1) The answer to the first question is simple, but it depends on the circumstances. If an unsigned number is shifted, fill in 0. If you have a signed number, you may be able to fill in 0 or sign bits. If you want to solve the problem of padding in the right shift, declare the variable to be unsigned, so that the vacated bits will be placed 0.

(2) The answer to the second question is also simple: if you move n bits, the number of bits shifted is less than 0, and must be less than N. This will not remove all the data in one operation.

For example, if the integer data is 32 bits and n is an integer, then n << 31 and N << 0 are all legitimate, and N << 32 and N <<-1 are illegal.

Note that the right shift of a signed integer is not equal to or divided by a bit, even if the vacated bits are signed. To prove this, we can think of -1 >> 1 impossible for 0.

Problem: bit-segment structure

struct RPR_ATD_TLV_HEADER
{
ULONG res1:6;
ULONG type:10;
ULONG res1:6;
ULONG length:10;
};

A bit structure is a special structure that is more convenient than bitwise operators when you need to access multiple bits of a byte or word by bit.

The general form of a bit structure definition is:

struct位结构名{
    数据类型 变量名: 整型常数;
    数据类型 变量名: 整型常数;
} 位结构变量; 

Where: Integer constants must be non-negative integers, the range is 0~15, representing the number of bits, that is, how many bits.

The variable name is a selection, and can be unnamed, so the requirement is to arrange the requirement.

For example, a bit structure is defined below.

struct{
    unsigned incon: 8; /*incon占用低字节的0~7共8位*/
    unsigned txcolor: 4;/*txcolor占用高字节的0~3位共4位*/
    unsigned bgcolor: 3;/*bgcolor占用高字节的4~6位共3位*/
    unsigned blink: 1; /*blink占用高字节的第7位*/
}ch;

The access of a bit structure member is the same as that of a struct member.

For example: Accessing the bgcolor member in the previous instance structure can be written as:

Ch.bgcolor

A bit structure member can be used with other struct members. bitwise access and Setup to facilitate & save

For example:

struct info{
    char name[8];
    int age;
    struct addr address;
    float pay;
    unsigned state: 1;
    unsigned pay: 1;
}workers;'

The structure of the previous example defines information about a worker. There are two bit structure members, each with only one digit, so that only one byte is saved but two information is stored, the first in the byte indicates the status of the worker, and the second indicates whether the pay has been paid. This shows that the use of bit structure can save storage space.

Be careful not to exceed the value limit

Related Article

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.