Detailed explanation of the use of binary complement and subscript operators in C + + _c language

Source: Internet
Author: User
Tags bitwise

Binary seek complement operator: ~
Grammar

~ cast-expression

Note
The binary counter operator (~), sometimes referred to as the "bitwise Inverse" operator, generates a bitwise binary counter for its operands. That is, each bit of 1 in the operand is 0 in the result. Instead, each bit of 0 in the operand is 1 in the result. The operands of the binary inverse code operator must be an integral type.
~ operator keyword
The compl operator is a text equivalent of ~. There are two ways to access the COMPL operator in a program: include the header file Iso646.h, or compile using/za.

Expre_one_complement_operator.cpp
//compile with:/EHsc
#include <iostream>

using namespace std ;

int main () {
  unsigned short y = 0xFFFF;
  cout << hex << y << Endl;
  y = ~y;  Take one ' s complement
  cout << hex << y << endl;
}

In this example, the new value assigned to Y is a binary inverse code of unsigned value 0xFFFF or 0x0000.
An integer elevation is performed on the integer operand, and the result type will be the type to which the operand will be elevated.

Subscript operator: []

 postfix-expression [Expression]

Note
An array index is specified, followed by the suffix expression of the subscript operator [], which can also be a primary expression.
Typically, the value represented by Postfix-expression is a pointer value (an array identifier), and expression is an integer value, including an enumeration type. However, syntactically, just one expression is a pointer type and the other is an integral type. The integer value can therefore be located in the postfix-expression position, and the pointer value can be in the square brackets of the expression or the subscript position. Consider the following code fragment:

  int narray[5] = {0, 1, 2, 3, 4};
  cout << narray[2] << Endl;      Prints "2"
  cout << 2[narray] << Endl;      Prints "2"

In the previous example, the expression narray[2] is the same as 2[narray]. The reason is that the result of the subscript expression e1[E2] is given as follows:

* ((E2) + (E1))

The expression generates an address that is not a E2 byte in an E1 address. Instead, the address is scaled to generate the next object in the array E2. For example:

Double adbl[2];

The address of adb[0] and adb[1] is 8 bytes-the size of an object of type double. Scaling based on the object type is done automatically by the C + + language and is defined in the Add and subtract operator of the operands of the pointer type.
The subscript expression can also have multiple subscripts, as follows:

expression1 [expression2] [Expression3] ...

The subscript expression is associated from left to right. First, the leftmost subscript expression Expression1[expression2] is evaluated. The address resulting from adding expression1 and expression2 constitutes a pointer expression, and then Expression3 is added to this pointer expression to form a new pointer expression, and so on, until the last subscript expression is added. When the last subscripted expression is evaluated, the indirection operator (*) is applied, unless the final pointer value is addressed to the array type.
An expression with multiple subscripts refers to the elements of a multidimensional array. A multidimensional array is an array of its elements. For example, the first element of a three-dimensional array is an array of two dimensions. The following example declares and initializes a simple two-dimensional array of characters:

Expre_subscript_operator.cpp
//compile with:/EHsc
#include <iostream>

using namespace std;
#define MAX_ROWS 2
#define MAX_COLS 2

int main () {
  char c[max_rows] [Max_cols] = {{' A ', ' B '}, {' C ', ' d '} } };
  for (int i = 0; i < max_rows i++) for
   (int j = 0; J < Max_cols; j +)
     cout << c[I [j] <&l T Endl;
}

Subscript and minus subscript
The first element of the array is element 0. The range of C + + arrays is from array[0] to array[size–1]. However, C + + supports subscript. The negative subscript must be within the bounds of the array, otherwise the result is unpredictable. The following code shows a positive group and a negative group subscript:

#include <iostream>
using namespace std;

int main () {
  int intarray[1024];
  for (int i = 0, j = 0; i < 1024; i++)
  {
    Intarray[i] = j + +;
  }

  cout << intarray[512] << endl;//
  
  int *midarray = &intArray[512];//Pointer to the middle of the A Rray

  cout << midarray[-256] << Endl;  256

  cout << intarray[-256] << Endl;//Unpredictable
}

A negative subscript in the previous line may produce a run-time error because it points to an address in memory that is 256 bytes lower than the origin of the array. The pointer Midarray is initialized to the midpoint of the intarray, so you can use positive and negative group indexes on it. Array subscript errors do not produce compile-time errors, but they can produce unpredictable results.
The subscript operator is exchangeable. Therefore, as long as the subscript operator is not overloaded (see overloaded operators), the expression Array[index] and Array[array are necessarily equivalent. The first form is the most common coding practice, but they are all valid.

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.