The usage of the colon (:) and double colon (: :) In c ++ and the c/c ++ bitwise domain struct

Source: Internet
Author: User
Tags domain list

1. colon (:) usage

(1) represents the definition of the location domain in the structure (that is, the variable occupies several bit spaces)

Typedef struct _ XXX {

Unsigned char a: 4;

Unsigned char c;

} XXX;

(2) The colon following the constructor acts as a segmentation function. It is a class-based method for assigning values to member variables. The initialization list is more suitable for the constant const type of member variables.

Struct _ XXX {

_ XXX (): y (0xc0 ){}

};

(3) The colons after public: AND private: indicate that all the Members defined later are public or private until the next "public:" or "private:" appears. "Private:" is the default processing.

(4) the class name is followed by the colon to define the inheritance of the class.

Class derived class name: Inheritance Method Base class Name

{

Member of the derived class

};

Inheritance Method: public, private, and protected. The default process is public.

 

2. Double colon (: :) usage

(1) "domain operator"
For example, A Class A is declared, and Class A declares A member function void f (), but the definition of f is not given in the class declaration, when f is defined outside the class,
Void A: f () indicates that the f () function is A member function of Class.

(2) It is used directly before the global function to indicate that it is a global function.
For example, in VC, you can add ::

(3) Reference member functions and variables, and scope member operators

For example, System: Math: Sqrt () is equivalent to System. Math. Sqrt ()

 

3. c/c ++ bitwise domain struct
When storing some information, it does not need to occupy a full byte, but only needs to occupy a few or one binary bit. For example, when storing a switch value, there are only two States: 0 and 1. Use one binary digit. To save storage space and simplify processing, the C language also provides a data structure called "bit domain" or "bit segment ". The so-called "bit field" refers to dividing the binary character in a byte into several different regions and showing the digits of each region. Each domain has a domain name, which allows operations by domain name in the program. In this way, several different objects can be represented by a byte binary field. 1. Definition of a bit field and description of a bit field variable the definition of a bit field is similar to that of a structure, in the form:

Struct bit domain structure name

{Bit domain list };

The format of the bit domain list is: type description Character Domain Name: Bit domain Length

For example:

Struct bs
{
Int a: 8;
Int B: 2;
Int c: 6;
};

The description of bitfield variables is the same as that of structure variables. You can first define and then describe, and define or directly describe these three methods. For example:

Struct bs
{
Int a: 8;
Int B: 2;
Int c: 6;
} Data;

It indicates that data is a bs variable, which occupies two bytes in total. Where a occupies 8 places, B occupies 2 places, and c occupies 6 places. The definitions of bit domains are described as follows:

1. A single-byte field must be stored in the same byte, and cannot span two bytes. If the remaining space of one byte is insufficient to store another domain, it should be stored from the next unit. You can also intentionally start a domain from the next unit. For example:

Struct bs
{
Unsigned a: 4
Unsigned: 0/* airspace */
Unsigned B: 4/* stored from the next unit */
Unsigned c: 4
}

In the definition of this bit field, a occupies 4 bits in the first byte, And the last 4 bits enter 0 to indicate that it is not used. B starts from the second byte and occupies 4 bits, and c occupies 4 bits.

2. Because the bit field cannot span two bytes, the length of the bit field cannot exceed the length of one byte, that is, it cannot exceed 8-bit binary.

3. A bit domain can be a non-bit domain name. In this case, it is only used for filling or adjusting the position. An anonymous domain cannot be used. For example:

Struct k
{
Int a: 1
Int: 2/* The two digits cannot be used */
Int B: 3
Int c: 2
};

From the above analysis, we can see that the bit field is essentially a structure type, but its members are allocated by binary.

Ii. Use of bit Domains

The usage of bit domains is the same as that of structure members. Generally, the form of bit domain variable name-bit domain name can be output in various formats.

Main (){
Struct bs
{
Unsigned a: 1;
Unsigned B: 3;
Unsigned c: 4;
} Bit, * PBit;
Bit. a = 1;
Bit. B = 7;
Bit. c = 15;
Printf ("% d, % d, % d/n", bit. a, bit. B, bit. c );
PBit = & bit;
PBit-> a = 0;
PBit-> B & = 3;
PBit-> c | = 1;
Printf ("% d, % d, % d/n", PBit-> a, PBit-> B, PBit-> c );
}

In the preceding example, the bit domain structure bs is defined. The three bit domains are a, B, and c. This section describes the bs type variable bit and the bs type pointer variable PBit. This indicates that pointers can also be used for bit fields.

The program's Lines 9, 10, and 11 assign values to the three single-digit domains. (Note that the value assignment cannot exceed the permitted range of the bit field) The program outputs the content of the three fields in integer format in line 1. Row 3 sends the bit address of the bit field variable to the pointer variable PBit. Row 14th re-assigns a value to bit field a as a pointer and assigns it to 0. Row 15th uses the compound bitwise operator "& =", which is equivalent to 7 in the original value of PBit-> B = PBit-> B & 3-bit Domain B, the bitwise AND operation result of 3 is 3 (111 & 011 = 011, And the decimal value is 3 ). Similarly, the Code uses the compound bitwise operation "| =" in line 1, which is equivalent to PBit-> c = PBit-> c | 1 and the result is 15. The program output the values of the three fields in the pointer mode in Row 3.

 


From Gary embedded development

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.