Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Whats the difference between bitwise and logical operators?

yvanyvan Posts: 1Member
[b][red]This message was edited by yvan at 2006-5-24 19:47:29[/red][/b][hr]
hello everyone,

Whats the difference when your using bitwise operators and logical operators in a conditional statements?

Bitwise Example:
if ((val==1)|(val==2)) printf("%d
",val);
else printf("False");

Logical Example:
if ((val==1)||(val==2)) printf("%d
",val);
else printf("False");

Which do you think is better and why is that? Thanks again for the replies ^_^


Comments

  • John SamuelsJohn Samuels Posts: 25Member
    : [b][red]This message was edited by yvan at 2006-5-24 19:47:29[/red][/b][hr]
    : hello everyone,
    :
    : Whats the difference when your using bitwise operators and logical operators in a conditional statements?
    :
    : Bitwise Example:
    : if ((val==1)|(val==2)) printf("%d
    ",val);
    : else printf("False");
    :
    : Logical Example:
    : if ((val==1)||(val==2)) printf("%d
    ",val);
    : else printf("False");
    :
    : Which do you think is better and why is that? Thanks again for the replies ^_^
    :
    :
    :


    What's better?? Is a Ferrari a better car than a Ford. It depends
    on how and where you use it for.
    If you're having some source handling some electronical hardware issues
    for example Port I/O configurations (10001110), it can be more convenient to use bitwise because the physical hardware is ordered that way. But if you're having to write code for a bank customers application an you're ding everything bitwise then it can become difficult to maintain.
    I suppose there could also be a compiler dependent performance issue,
    but I don't know enough about that.

    Greetings from John Samuels


  • shaolin007shaolin007 Posts: 1,018Member
    : [b][red]This message was edited by yvan at 2006-5-24 19:47:29[/red][/b][hr]
    : hello everyone,
    :
    : Whats the difference when your using bitwise operators and logical operators in a conditional statements?
    :
    : Bitwise Example:
    : if ((val==1)|(val==2)) printf("%d
    ",val);
    : else printf("False");
    [red] This statement is useless because val can not be 2 values at the same time in a bitwise operation. You are 'or'ing the bits.[/red]

    : Logical Example:
    : if ((val==1)||(val==2)) printf("%d
    ",val);
    : else printf("False");
    [red]This is evaluating whether val is either equal to 1 or 2 and if so do something about it.[/red]

    : Which do you think is better and why is that? Thanks again for the replies ^_^
    :
    :
    :
    [green]
    There is no better than the other because they are not the same. Logical operators evaluate expressions for truth while bitwise evaluate individual bits for truth.
    [/green]

    [code]
    //Example using bitwise operator & to determine if number is odd

    short int number=3;
    if (number & 1)
    true=odd=1
    false=even=0

    0000 0000 0000 0011
    &
    0000 0000 0000 0001
    -------------------
    0000 0000 0000 0001 true
    [/code]



  • DonotaloDonotalo Posts: 715Member
    : [b][red]This message was edited by yvan at 2006-5-24 19:47:29[/red][/b][hr]
    : hello everyone,
    :
    : Whats the difference when your using bitwise operators and logical operators in a conditional statements?
    :
    : Bitwise Example:
    : if ((val==1)|(val==2)) printf("%d
    ",val);
    : else printf("False");
    :
    : Logical Example:
    : if ((val==1)||(val==2)) printf("%d
    ",val);
    : else printf("False");
    :
    : Which do you think is better and why is that? Thanks again for the replies ^_^
    :
    :
    :
    [purple]
    bitwise operators are operated on bits. for a bitwise OR operator, both of the left hand side and right hand side values/expressions are evaluated. then the 'evaluated values' are ORed together.[code]
    int a = 33, b = 40, c;
    c = a | b;

    /***********************
    33 => 100001 a
    40 => 101000 b
    ------------ OR
    41 => 101001 c
    ***********************/
    [/code]
    on the other hand, if expressions are used as the operands of bitwise operator OR:[code]
    int val = 2, a;
    a = (val == 1) | (val == 2);
    printf("%d
    ", a);
    [/code]
    then the output for the above code is always 1 if val is either 1 or 2, and the output is 0 otherwise. because, in the above case, (val == 1) is evaluated. if this is true, then (val==1) is considered 1, otherwise 0. similarly, (val == 2) is evaluated and is considered 1 or 0 depending if it is true or false. then these two 0 or 1 are ORed together.

    logical operators work a little bit different way. say, the code is

    [b](val==1) || (val==2)[/b]

    the above is a logical expression, whose value is either true or false. to evaluate the above expression, first (val==1) is checked. if this is found true, then [b]the entire expression become true and no further checking is required to check whether (val==2) or not, computer skips checking the right hand side,[/b] since they are ORed. if (val==1) is found false, then the value of the expression depends on whether (val==2) is true or false.

    similarly, for logical AND operator,

    [b](a==1) && (b==2)[/b]

    first (a==1) is evaluated. if this is found false, then [b]the entire expression become false and computer skips checking (b==2),[/b] since they are ANDed together. if (a==1) is found true, then the checking (b==2) is required to evaluate the whole expression.

    so use logical operators when "if this AND that" or "if this OR that" kind of checking is required. :-) go for the bitwise operator when u really need a new bit stream. remember, logical operators skip evaluating in certain conditions where bitwise operators always evaluate both sides.
    [/purple]
    [hr][purple]~Donotalo()[/purple]

Sign In or Register to comment.