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

Giuseppe Cardone giuseppe.cardone at studio.unibo.it
Thu Apr 22 11:02:22 PDT 2010


this "problem" is not related to MSP430, nesC or TinyOS. Rather it's
related to the C standard. The point 4 from section 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:


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.


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

More information about the Tinyos-help mailing list