What Is the "--≫" Operator in C++

What is the -- operator in C++?

--> is not an operator. It is in fact two separate operators, -- and >.

The conditional's code decrements x, while returning x's original (not decremented) value, and then compares the original value with 0 using the > operator.

To better understand, the statement could be written as follows:

while( (x--) > 0 )

What is the operator in C++?

Those are user-defined literals. They allow you to create stuff like std::string, std::chrono::durations or any user defined type (you can make your own literals) in place:

auto str = "Hello"s; // str is std::string("Hello")
auto sec = 5s; // sec is 5 std::chrono::seconds

A list of the literal-operators provided by the standard library and their documentation can be found at the bottom of the documentation page I linked.

What is the = operator in C?

The line:

while( a[ 0xFULL?'\0':-1:>>>=a<:!!0X.1P1 ] )

contains the digraphs :> and <:, which translate to ] and [ respectively, so it's equivalent to:

while( a[ 0xFULL?'\0':-1 ] >>= a[ !!0X.1P1 ] )

The literal 0xFULL is the same as 0xF (which is hex for 15); the ULL just specifies that it's an unsigned long long literal. In any case, as a boolean it's true, so 0xFULL ? '\0' : -1 evaluates to '\0', which is a character literal whose numerical value is simply 0.

Meanwhile, 0X.1P1 is a hexadecimal floating point literal equal to 2/16 = 0.125. In any case, being non-zero, it's also true as a boolean, so negating it twice with !! again produces 1. Thus, the whole thing simplifies down to:

while( a[0] >>= a[1] )

The operator >>= is a compound assignment that bit-shifts its left operand right by the number of bits given by the right operand, and returns the result. In this case, the right operand a[1] always has the value 1, so it's equivalent to:

while( a[0] >>= 1 )

or, equivalently:

while( a[0] /= 2 )

The initial value of a[0] is 10. After shifting right once, it become 5, then (rounding down) 2, then 1 and finally 0, at which point the loop ends. Thus, the loop body gets executed three times.

Why does the arrow (-) operator in C exist?

I'll interpret your question as two questions: 1) why -> even exists, and 2) why . does not automatically dereference the pointer. Answers to both questions have historical roots.

Why does -> even exist?

In one of the very first versions of C language (which I will refer as CRM for "C Reference Manual", which came with 6th Edition Unix in May 1975), operator -> had very exclusive meaning, not synonymous with * and . combination

The C language described by CRM was very different from the modern C in many respects. In CRM struct members implemented the global concept of byte offset, which could be added to any address value with no type restrictions. I.e. all names of all struct members had independent global meaning (and, therefore, had to be unique). For example you could declare

struct S {
int a;
int b;
};

and name a would stand for offset 0, while name b would stand for offset 2 (assuming int type of size 2 and no padding). The language required all members of all structs in the translation unit either have unique names or stand for the same offset value. E.g. in the same translation unit you could additionally declare

struct X {
int a;
int x;
};

and that would be OK, since the name a would consistently stand for offset 0. But this additional declaration

struct Y {
int b;
int a;
};

would be formally invalid, since it attempted to "redefine" a as offset 2 and b as offset 0.

And this is where the -> operator comes in. Since every struct member name had its own self-sufficient global meaning, the language supported expressions like these

int i = 5;
i->b = 42; /* Write 42 into `int` at address 7 */
100->a = 0; /* Write 0 into `int` at address 100 */

The first assignment was interpreted by the compiler as "take address 5, add offset 2 to it and assign 42 to the int value at the resultant address". I.e. the above would assign 42 to int value at address 7. Note that this use of -> did not care about the type of the expression on the left-hand side. The left hand side was interpreted as an rvalue numerical address (be it a pointer or an integer).

This sort of trickery was not possible with * and . combination. You could not do

(*i).b = 42;

since *i is already an invalid expression. The * operator, since it is separate from ., imposes more strict type requirements on its operand. To provide a capability to work around this limitation CRM introduced the -> operator, which is independent from the type of the left-hand operand.

As Keith noted in the comments, this difference between -> and *+. combination is what CRM is referring to as "relaxation of the requirement" in 7.1.8: Except for the relaxation of the requirement that E1 be of pointer type, the expression E1−>MOS is exactly equivalent to (*E1).MOS

Later, in K&R C many features originally described in CRM were significantly reworked. The idea of "struct member as global offset identifier" was completely removed. And the functionality of -> operator became fully identical to the functionality of * and . combination.

Why can't . dereference the pointer automatically?

Again, in CRM version of the language the left operand of the . operator was required to be an lvalue. That was the only requirement imposed on that operand (and that's what made it different from ->, as explained above). Note that CRM did not require the left operand of . to have a struct type. It just required it to be an lvalue, any lvalue. This means that in CRM version of C you could write code like this

struct S { int a, b; };
struct T { float x, y, z; };

struct T c;
c.b = 55;

In this case the compiler would write 55 into an int value positioned at byte-offset 2 in the continuous memory block known as c, even though type struct T had no field named b. The compiler would not care about the actual type of c at all. All it cared about is that c was an lvalue: some sort of writable memory block.

Now note that if you did this

S *s;
...
s.b = 42;

the code would be considered valid (since s is also an lvalue) and the compiler would simply attempt to write data into the pointer s itself, at byte-offset 2. Needless to say, things like this could easily result in memory overrun, but the language did not concern itself with such matters.

I.e. in that version of the language your proposed idea about overloading operator . for pointer types would not work: operator . already had very specific meaning when used with pointers (with lvalue pointers or with any lvalues at all). It was very weird functionality, no doubt. But it was there at the time.

Of course, this weird functionality is not a very strong reason against introducing overloaded . operator for pointers (as you suggested) in the reworked version of C - K&R C. But it hasn't been done. Maybe at that time there was some legacy code written in CRM version of C that had to be supported.

(The URL for the 1975 C Reference Manual may not be stable. Another copy, possibly with some subtle differences, is here.)

Why can't I use the ^ operator in C?

It works just fine and means bitwise XOR. That is, 1^2 gives 3.

Unfortunately C doesn't provide a function to take power of integers. This is a known flaw of the language. You have to roll out such a function yourself either by using multiplication in a loop, or use the slow floating point function pow.

What does the |= operator mean in C++?

Assuming you are using built-in operators on integers, or sanely overloaded operators for user-defined classes, these are the same:

a = a | b;
a |= b;

The '|=' symbol is the bitwise OR assignment operator. It computes the value of OR'ing the RHS ('b') with the LHS ('a') and assigns the result to 'a', but it only evaluates 'a' once while doing so.

The big advantage of the '|=' operator is when 'a' is itself a complex expression:

something[i].array[j]->bitfield |= 23;

vs:

something[i].array[i]->bitfield = something[i].array[j]->bitfield | 23;

Was that difference intentional or accidental?

...

Answer: deliberate - to show the advantage of the shorthand expression...the first of the complex expressions is actually equivalent to:

something[i].array[j]->bitfield = something[i].array[j]->bitfield | 23;

Similar comments apply to all of the compound assignment operators:

+= -= *= /= %=
&= |= ^=
<<= >>=

Any compound operator expression:

a XX= b

is equivalent to:

a = (a) XX (b);

except that a is evaluated just once. Note the parentheses here - it shows how the grouping works.

What does this = operator mean in C?

The expression set >>= 1; means set = set >> 1; that is right shift bits of set by 1 (self assigned form of >> bitwise right shift operator check Bitwise Shift Operators).

Suppose if set is:

BIT NUMBER    31   n=27        m=17                 0
▼ ▼ ▼ ▼
set = 0000 1111 1111 1110 0000 0000 0000 0000

Then after set >> = 1; variable set becomes:

BIT NUMBER    31   n=26        m=16                 0
▼ ▼ ▼ ▼
set = 0000 0111 1111 1111 0000 0000 0000 0000

Notice the bits number shifted.

Note a interesting point: Because set is unsigned long so this >> operation should be logical shift( unsigned shift) a logical shift does not preserve a number's sign bit.

Additionally, because you are shifting all bits to right (towards lower significant number) so one right shift is = divide number by two.

check this code (just to demonstrate last point):

int main(){
unsigned long set = 268304384UL;
set >>= 1;
printf(" set :%lu \n", set);
set = 268304384UL;
set /= 2;
printf(" set :%lu \n", set);
return 1;
}

And output:

 set :134152192 
set :134152192

(note: its doesn't means >> and / are both same)

Similarly you have operator <<= for left shift, check other available Bitwise operators and Compound assignment operators, also check section: bit expressions and difference between: signed/arithmetic shift and unsigned shift.

Operator ~ in c programming language

The ~ operator in c is the NOT bitwise operator. So, in your example

   main()
{
printf("%d",~5);
}

will print

-6

The bits work as follows.

5 = 0000 0101

When you take the NOT of a byte you flip all the 1's and 0's making the new number

-6 = 1111 1010.



Related Topics



Leave a reply



Submit