Operator ~ with complex arguments

J

jacob navia

Consider this code:

< code >
#include <complex.h>
int main(void)
{
double complex c = 0.4+2.9I;
c = ~c;
}
< end code >

Is this legal?

The standard says about the ~ operator:
<quote>
6.5.5.3
The result of the ~ operator is the bitwise complement of its (promoted)
operand (that is, each bit in the result is set if and only if the
corresponding bit in the converted operand is not set). The integer
promotions are performed on the operand, and the result has the
promoted type. If the promoted type is an unsigned type, the expression
~E is equivalent to the maximum value representable in that type minus
E.
< end quote>

This means that I should "promote" the complex to double,
ignoring the imaginary part, then converting the resulting double
to integer, NOT to unsigned integer.

0.4 + 2.9I --> 0.4 --> 0

THEN I should apply ~ to that, resulting into the integer -1. Then,
I should convert that -1 to double, then to double complex.

This looks quite hairy. Did I get it right?

Thanks in advance for your time.
 
W

Wojtek Lerch

jacob navia said:
Consider this code:

< code >
#include <complex.h>
int main(void)
{
double complex c = 0.4+2.9I;
c = ~c;
}
< end code >

Is this legal?

It's a constraint violation.

6.5.3.3 Unary arithmetic operators

Constraints

1 The operand of the unary + or - operator shall have arithmetic type; of
the ~ operator, integer type; of the ! operator, scalar type.
 
J

jacob navia

Wojtek said:
It's a constraint violation.

6.5.3.3 Unary arithmetic operators

Constraints

1 The operand of the unary + or - operator shall have arithmetic type;
of the ~ operator, integer type; of the ! operator, scalar type.

AARGH I oversaw that, I just looked at the definition of the
operator. Thanks
 
J

jacob navia

Jack said:
Unless I am missing, applying the unary ~ operator to anything other
than an integer operand is a constraint violation, requiring a
diagnostic:

"6.5.3.3 Unary arithmetic operators
Constraints
1 The operand of the unary + or - operator shall have arithmetic type;
of the ~ operator, integer type; of the ! operator, scalar type."

I haven't actually used the <complex.h> types or functions myself, but
I just took a quick look at that section of the standard and see
nothing there that overrules this constraint for complex types.

Of course, once you emit the required diagnostic for the constraint
violation, you can do whatever you want with the expression.

Or am I missing something?

No I do not think so. I oversaw that sentence and started reading at the
definition of the operator. My fault. Thanks for your time
 
L

lawrence.jones

In comp.std.c jacob navia said:
AARGH I oversaw that, I just looked at the definition of the
operator. Thanks

When consulting the C standard, *always* look at all the parents of the
applicable subclause, too. They frequently contain vital information.
 
C

cr88192

jacob navia said:
Consider this code:

< code >
#include <complex.h>
int main(void)
{
double complex c = 0.4+2.9I;
c = ~c;
}
< end code >

Is this legal?

not as per the standard, but this was a good place IMO to put a conjugate
operator...
~(a+bi)=(a-bi)
was also used for quaternions.

but, at least for complexes, the standard provides a function for this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top