Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

so my question is what is the order of operations for bitwise operators << & |

and also to see if my logic is right with the problem below

(x03 << x08)+ x00 = 300

((x300 >> x8) | (x300 & x7F) << 8)+1 = 301

share|improve this question
4  
"what is the order of operations for bitwise operators" - depends on the environment. When there is potential ambiguity, use parentheses! –  J. M. Oct 24 '11 at 16:01
    
thank you, i see i overlooked that when i typed up the problem –  owen gerig Oct 24 '11 at 16:08
    
so then the second one actually equals 4? instead of 301. –  owen gerig Oct 24 '11 at 20:54
    
This question would probably fit better at StackOverflow; it is at best only tangentially related to mathematics. –  Ilmari Karonen Nov 28 '11 at 18:38
add comment

2 Answers 2

Your notation suggests that you're specifically interested in arithmetic operator precedence in the C programming language, or possibly in one of the many languages based on it (such as C++, C#, Java, etc.). As such, its relevance to the Mathematics Stack Exchange site is somewhat limited.

However, it should be noted that the operator precedence rules in C are mostly designed to match established mathematical convention, so that e.g. multiplication (*) and division (/) take precedence over addition (+) and subtraction (-), and that those operators themselves are appropriately left-associative, such an expression such as a - b * c + d / e is parsed as $$a - b \cdot c + d / e = (a - (b \cdot c)) + (d / e),$$ as one would expect from elementary school math lessons.

C does, however, introduce several operators that are not commonly encountered in elementary mathematics. These include the bit shift operators << and >>, which may be defined mathematically as $a \ll b = a \cdot 2^b$ and $a \gg b = \lfloor a / 2^b \rfloor$.

Curiously, even though the mathematical definition above suggests that the bit shift operators ought to have precedence equal to or higher than multiplication (since they fundamentally correspond to multiplication and exponentiation), that is not how they are defined in C; instead, their precedence is defined to be just below that of addition and subtraction, so that e.g. a + b << c - d is actually parsed as (a + b) << (c - d), which evaluates as $(a + b) \cdot 2^{(c - d)}$.

The bitwise AND (&) and OR (|) operations, which mathematically correspond to multiplication and addition in $(\mathbb Z/2\mathbb Z)^n$, are defined in C to have even lower precedence than the bit shifts (and, indeed, even lower than the comparison operators!), which can lead to such unexpected parsing results as a + b & c being equal to (a + b) & c, and even a == b & c evaluating as (a == b) & c (where a == b evaluates to $1$ if $a = b$ and to $0$ otherwise). On the other hand, this low precence does mean that, for example, the idiom a | 1 << b (where most C programmers would probably wrap the shift in parentheses, just to be safe) for setting the $b$-th bit of $a$ does, in fact, get parsed as a | (1 << b).

Among themselves, & takes precedence over |, as one might expect by the mathematical analogy with multiplication and addition.


As for the second half of your question, you got one out of two right: yes,

(0x03 << 0x08) + 0x00 = 0x300

but

((0x300 >> 0x8) | (0x300 & 0x7F) << 8) + 1 = 4

or, as mathematicians might write them,

$$\begin{align*} &(3_{16} \ll 8_{16}) + 0_{16} \\ =& (3 \cdot 2^8) + 0 \\ =& (3 \cdot 100_{16}) + 0 \\ =& 300_{16} + 0 \\ =& 300_{16} \end{align*}$$

and

$$\begin{align*} & ((300_{16} \gg 8_{16}) \oplus (300_{16} \odot \mathrm{7F}_{16}) \ll 8) + 1 \\ =& ((300_{16} \gg 8_{16}) \oplus ((300_{16} \odot \mathrm{7F}_{16}) \ll 8)) + 1 \\ =& (3_{16} \oplus (0 \ll 8)) + 1 \\ =& (3_{16} \oplus 0) + 1 \\ =& 3_{16} + 1 \\ =& 4 \end{align*}$$

share|improve this answer
add comment
up vote 0 down vote accepted

parenthesizes determine operations

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.