Doubt in displacement of bits in C

Asked

Viewed 458 times

5

My question is regarding the following excerpt from a code:

    #include <stdio.h>

    int main(void){

    int teste = 0, x0 = 0, x1 = 0, x2;

    x2 = 1;
    teste = ((x0|x2) | (x1|x2) << 1);

    printf("Valor de teste: %d   ", teste);

    }

When I compile this code the value displayed for the test variable is 3, yet I do not understand what the following passage does, teste = ((x0|x2) | (x1|x2) << 1); You could help me?

  • Windows calculator in "programmer mode" can greatly help the visualization of these bit displacement functions

2 answers

5

This section works with the binary of the numbers, you must understand the functioning of each one:

1) The symbol "|" means "OR inclusive" and what it does is as follows::

Makes the value of the bit equal to 1 if one or both of the corresponding bits in the operands is 1 and 0 in the other cases. Example: if the variable var has the value 12 (00001100) and doing the operation with 6 (00000110), the result, var | 6, will be 14 (00001110).

2) The symbol "<<" means "left shift" and what it does is as follows::

Move the left operand bits in the value given by the right operand to the left operand. It is equal to the multiplication by the power of 2 given by the latter. Example: if variable var has value 3 (00000011), after var << 2, it will be 12 (00001100).

So what your code is doing is about that:

teste = ((x0|x2) | (x1|x2) << 1);
teste = ((00000000|00000001) | (00000000|00000001) << 00000001);
teste = (00000001 | 00000001 << 00000001);
//O operador << tem precedência, então neste momento ele é executado primeiro
teste = (00000001 | 00000001 << 00000001);
teste = (00000001 | 00000010);
teste = (00000011);
teste = (3);

If you still have questions with any other type of operator, this page http://www.mspc.eng.br/info/cpp_oper_10.shtml explains well the functioning of each.

3


When so separate each operation to see each step taking place, as you would on paper.

#include <stdio.h>

int main(void) {
    int x0 = 0, x1 = 0, x2 = 1;
    printf("Valor de x0 | x2: %d\n", x0 | x2);
    printf("Valor de x1 | x2: %d\n", x1 | x2);
    printf("Valor de (x1 | x2) << 1: %d\n", (x1 | x2) << 1);
    printf("Valor de (x0 | x2) | (x1 | x2) << 1: %d\n", (x0 | x2) | (x1 | x2) << 1);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

By the truth table an OR operator results in 1 whenever at least one of the operands is 1. This is what occurs of the two subexpressions that use the or (|).

Turns out that by operator precedence table the << is executed before. This is the bit offset operator (see links below). Then it is prompting to shift a bit to the left. The binary number 1 is 00000001. If you move a bit it would be 00000010. And this decimal number is 2.

Finally the last calculation will be done which is again an OR. Then there will be a comparison between 00000001 and 00000010, the results of the two expressions on each side. Then the result will always be 1 when one of it is 1. So it will result in 00000011 because it has 1 in the last bit in an operand and has 1 in the penultimate bit of the second operand. The 00000011 decimal is 3, the final result.

Examples and additional information:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.