Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Bitwise operator

Bitwise operator

Scheduled Pinned Locked Moved C#
help
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    humayunlalzad
    wrote on last edited by
    #1

    Whats the difference between

    if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
    {
    //Code

            }
    

    and

    if(e.Button == MouseButtons.Right)
    //Code

    They both work the same I am unable to understand the use of the logical operator & and ALSO when I change the & with && it wont work Can anyone help

    L G L 3 Replies Last reply
    0
    • H humayunlalzad

      Whats the difference between

      if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
      {
      //Code

              }
      

      and

      if(e.Button == MouseButtons.Right)
      //Code

      They both work the same I am unable to understand the use of the logical operator & and ALSO when I change the & with && it wont work Can anyone help

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      humayunlalzad wrote:

      Can anyone help

      Yes, you can by learning what bits are and how the different bit operations work. Perhaps you should start here[^]

      led mike

      1 Reply Last reply
      0
      • H humayunlalzad

        Whats the difference between

        if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
        {
        //Code

                }
        

        and

        if(e.Button == MouseButtons.Right)
        //Code

        They both work the same I am unable to understand the use of the logical operator & and ALSO when I change the & with && it wont work Can anyone help

        G Offline
        G Offline
        Gideon Engelberth
        wrote on last edited by
        #3

        They don't actually work the same. If that event was raised with both right and left mouse buttons pressed (eg: e.Button = (MouseButtons.Right | MouseButtons.Left)) then the two snippets would be different. The difference is that the first one checks to see if a particular bit in a value is set and the second checks to see if the value is exactly equal to some other value. I assume that when you switch & with && you got a compiler error. The single operators & and | are used to perform bitwise operations. The double operators && and || are used to perform operations on boolean values.

        1 Reply Last reply
        0
        • H humayunlalzad

          Whats the difference between

          if ((e.Button & MouseButtons.Right) == MouseButtons.Right)
          {
          //Code

                  }
          

          and

          if(e.Button == MouseButtons.Right)
          //Code

          They both work the same I am unable to understand the use of the logical operator & and ALSO when I change the & with && it wont work Can anyone help

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi,

          humayunlalzad wrote:

          the logical operator &

          No, && would be a logical operator, & is a bitwise operator The following is similar to your code (just assume MouseButtons.Right=1), but may be easier to understand:

          for(int i=int.MinValue; i<int.MaxValue; i++) {
          if ((i & 1) == 1) Console.WriteLine(i.ToString()+" is an odd number");
          if (i == 1) Console.WriteLine(i.ToString()+" equals 1");
          }

          i & 1 ANDs the bits of i with the bits of 1 (which is all zeroes except for the rightmost bit); the result is either 1 or 0, and depends solely on the lowest bit of i. As a result, the former test checks only a single bit (and hits many times), the latter checks all bits in variable i (and hits just once). :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          Fixturized forever. :confused:


          H 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi,

            humayunlalzad wrote:

            the logical operator &

            No, && would be a logical operator, & is a bitwise operator The following is similar to your code (just assume MouseButtons.Right=1), but may be easier to understand:

            for(int i=int.MinValue; i<int.MaxValue; i++) {
            if ((i & 1) == 1) Console.WriteLine(i.ToString()+" is an odd number");
            if (i == 1) Console.WriteLine(i.ToString()+" equals 1");
            }

            i & 1 ANDs the bits of i with the bits of 1 (which is all zeroes except for the rightmost bit); the result is either 1 or 0, and depends solely on the lowest bit of i. As a result, the former test checks only a single bit (and hits many times), the latter checks all bits in variable i (and hits just once). :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Fixturized forever. :confused:


            H Offline
            H Offline
            humayunlalzad
            wrote on last edited by
            #5

            I do understand the behaviour of bitwise operatores on Integers. But what I still dont understand is

            e.Button

            how is that an Integer, or what bits does it have or how do we know those bits. I assumed that MouseButton is an enum and the bitwise operator would work on its underlying byte or int value. And thanx for the help

            L 1 Reply Last reply
            0
            • H humayunlalzad

              I do understand the behaviour of bitwise operatores on Integers. But what I still dont understand is

              e.Button

              how is that an Integer, or what bits does it have or how do we know those bits. I assumed that MouseButton is an enum and the bitwise operator would work on its underlying byte or int value. And thanx for the help

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, MouseEventArgs.Button returns a MouseButtons Enumeration, which basically is an int with some bits set. I suggest you read up on enums, and the Flags attribute if you want to know the specific details. :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              Fixturized forever. :confused:


              H 1 Reply Last reply
              0
              • L Luc Pattyn

                Hi, MouseEventArgs.Button returns a MouseButtons Enumeration, which basically is an int with some bits set. I suggest you read up on enums, and the Flags attribute if you want to know the specific details. :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                Fixturized forever. :confused:


                H Offline
                H Offline
                humayunlalzad
                wrote on last edited by
                #7

                Thanx a lot everything is clear now. You were a great help.

                L 1 Reply Last reply
                0
                • H humayunlalzad

                  Thanx a lot everything is clear now. You were a great help.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  you're welcome. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  Fixturized forever. :confused:


                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups