Jump to content


Photo

Tilde and order of operations


  • Please log in to reply
2 replies to this topic

#1 OMGCarlos

OMGCarlos

    GMC Member

  • GMC Member
  • 329 posts
  • Version:GM:HTML5

Posted 21 October 2011 - 07:04 PM

I'm building a parser/interpreter for GM:HTML5 and working out expressions. I ran into this symbol ~ and have never seen this before. What does it mean to "negate the next value bitwise"? I assume it does something like:
0010 -> 1101
but when I test it I really don't know what I'm looking at...

Also, if anyone knows, what is the complete oder of operations to include bitwise operators? I'm going off of this:
||
&&
|
^
&
=, !=
<, >, <=, >=
<<, >>
!, ~
+, -
*, /, mod, div
function, variable
  • 0

#2 IceMetalPunk

IceMetalPunk

    InfiniteIMPerfection

  • Retired Staff
  • 9259 posts
  • Version:Unknown

Posted 21 October 2011 - 07:43 PM

You are correct. Bitwise negation (~) takes all the 0 bits and converts them to 1's and vice-versa. To convert binary to decimal (the number system you're familiar with) yourself, follow this process:

1) Start from the right side. The right-most bit is bit 0, then bit 1, etc.
2) If bit N is b[N] (and either 0 or 1), then going from right-to-left, add b[N] * 2^N.

When you're done, the resulting number is the same number the bit string represents. So let's say you have the value 3. In binary, that's 00000011. So ~3 = 11111100. Converting that to decimal, you get 252. So ~3=252.

for single-bytes, you may notice that ~X = 255-X, but for multi-byte values that doesn't hold.

-IMP
  • 0

#3 OMGCarlos

OMGCarlos

    GMC Member

  • GMC Member
  • 329 posts
  • Version:GM:HTML5

Posted 21 October 2011 - 10:07 PM

Awesome response, answered my question and more. Thanks!
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users