C ++ expression

Source: Internet
Author: User

* Sizeof
Three Forms
Sizeof (type name); // Note: There is no sizeof typename so sizeof int error, only sizeof (INT );
Sizeof (object); // sizeof (I _rec );
Sizeof object; // sizeof I _rec;

When the object is an array, the length of the entire array is returned, not the number of elements.
Int Ia [] = {2, 3, 4 };
Int size = sizeof (IA); // returns 4*3 = 12
When the object is a pointer, the system returns the memory space occupied by the pointer.
Zeof (short *); // returns 4, space occupied by the pointer itself
When an object is referenced, the space of the object to be referenced is returned.
Sizeof (short &) // returns 2

* Comma Operator
Int A = 0, B, c;
Int xx = (1 = 1)
? (A = 8, B = 7)
: (B = 3, C = 2 );
The result is the value of the rightmost expression result, above xx = 7

* Bit operations
Constructing a bit group can be obtained by using the integer displacement method. For example, 1 <27 is to set 27th bits to 1
To set a value of 1:
Unsigned int quizl = 1;
Quizl | = 1 <27; // 1 <27 indicates that the first 27th bits are 1, the other bits are 0, and quizl is the same as or. The third bits are set to 1, other BITs remain unchanged.
To set a value of 0:
Quizl & = ~ (1 <27 )//~ (1 <27) construct the 27th bits as 0, the other BITs as 1, and the sum of quizl. The 27th bits are set as 0, and the other BITs remain unchanged.
To determine whether a value is 0 or 1:
Bool haspassed = quizl & (1 <27); // 1 <27 constructs the 27th bits as 1, and the other BITs as 0, all except 27 BITs are set to 0. If the 27th bits of quizl are also 0,
Then, the result of quizl & (1 <27); is also 0. Otherwise, if it is not 0, you can determine whether the 27th-bit value is 1.

The following is an example of a bit operation:
Class bitset
{
Public:
Bool bit_on (unsigned int UI, int POS );

Bool bit_off (unsigned int UI, int POS );

Void bit_turn_on (unsigned Int & UI, int POS );

Void bit_turn_off (unsigned Int & UI, int POS );

};
Inline bool bitset: bit_on (unsigned int UI, int POS)
{
Return UI & (1 <27 );
}

Inline bool bitset: bit_off (unsigned int UI, int POS)
{
Return! Bit_on (ui, POS );
}

Inline void bitset: bit_turn_on (unsigned Int & UI, int POS)
{
Ui | = (1 <POS );
}

Inline void bitset: bit_turn_off (unsigned Int & UI, int POS)
{
UI ~ (1 <POS );
}

In addition, from the perspective of generic programming, the code can be rewritten as follows:
Template <class type>
Class bitset
{
Public:
Bool bit_on (type UI, int POS );

Bool bit_off (type UI, int POS );

Void bit_turn_on (type & UI, int POS );

Void bit_turn_off (type & UI, int POS );

};

Template <class type>
Inline bool bitset <type >:: bit_on (type UI, int POS)
{
Return UI & (1 <POS );
}

Template <class type>
Inline bool bitset <type >:: bit_off (type UI, int POS)
{
Return! Bit_on (ui, POS );
}

Template <class type>
Inline void bitset <type >:: bit_turn_on (type & UI, int POS)
{
Ui | = (1 <POS );
}

Template <class type>
Inline void bitset <type >:: bit_turn_off (type & UI, int POS)
{
UI ~ (1 <POS );
}
Example 1:
Unsigned char quizl = 0;
Bitset <unsigned char> bit;
Bit. bit_turn_on (quizl, 5 );
// Bit. bit_turn_off (quizl, 5 );
Cout <bit. bit_on (quizl, 5) <Endl;
Example 2:
Unsigned int quizl = 0;
Bitset <unsigned int> bit;
Bit. bit_turn_on (quizl, 5 );
// Bit. bit_turn_off (quizl, 5 );
Cout <bit. bit_on (quizl, 5) <Endl;

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.