[Tinyos-help] Bitwise Not (Complement) Operator @ msp430 / nesc 1.3.1

Michael Schippling schip at santafe.edu
Thu Apr 22 15:51:08 PDT 2010


I'm not sure your interpretation is correct -- this phrase:

"...the expression ~E is equivalent to the maximum value
representable in that type minus E..."

Seems to say that:   ~E == (MAX - E)
where MAX is 255 for an unsigned byte.
Which is exactly what the OP was trying to do:  255 - E

If that isn't the case I don't think I could do bit setting:
	mask |= BITFIELD;
and resetting:
	mask &= ~BITFIELD;

There might be some sign extension foo on signed types,
but I think the ~bitflip deals with that automagically...
MS


Giuseppe Cardone wrote:
> Hi,
> 
> this "problem" is not related to MSP430, nesC or TinyOS. Rather it's
> related to the C standard. The point 4 from section 6.5.3.3 of the C99
> standard reads:
> 
> "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."
> 
> (I'm unable to find the C89 standard at this moment, but it says
> pretty much the same things).
> 
> Therefore if you do:
> 
> ~a
> 
> the type is promoted to signed int, and its value is -6. Since 255-a =
> 250, the results are different and your comparison returns false.
> 
> Regards.
> 
> 
> --
> Giuseppe Cardone
> 
> 
> 
> On Thu, Apr 22, 2010 at 5:46 PM, Juchli Bruno HSLU T&A
> <bruno.juchli at hslu.ch> wrote:
>> ---------- Forwarded message ----------
>> From: Juchli Bruno HSLU T&A <bruno.juchli at hslu.ch>
>> To: "tinyos-help at millennium.berkeley.edu" <tinyos-help at millennium.berkeley.edu>
>> Date: Thu, 22 Apr 2010 17:46:45 +0200
>> Subject: Bitwise Not (Complement) Operator @ msp430 / nesc 1.3.1
>> Dear All,
>>
>> i've had some trouble debugging my TinyOS 2.1.1 application today. I've had some code that essentially was the same as:
>>
>> uint8_t a = 5;
>>
>> what struck me, was that the following returned FALSE:
>>
>> (~a == (255-a))
>>
>>
>> { Note:
>> b00000101 = 5
>> b11111010 = ~5 = 250 = (255-5)
>> }
>>
>> Did i miss something or is this a (un-)known problem?
>>
>> I ended up using (255-b) instead of ~b.
>>
>> regards,
>>  Bruno Juchli
> 
> _______________________________________________
> Tinyos-help mailing list
> Tinyos-help at millennium.berkeley.edu
> https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help


More information about the Tinyos-help mailing list