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 / C++ / MFC
  4. [MISC] - Operators - Meaning of operator &

[MISC] - Operators - Meaning of operator &

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 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.
  • T Offline
    T Offline
    Thang
    wrote on last edited by
    #1

    Dear everybody, I have a question about the meaning of & operator in the following expression: ((WORD)(((DWORD)(l) >> 16) & 0xFFFF)) 1) What role does the & operator do here ? 2) Why do programmers often use & with hexadecimal, not integer or long integer or double ? I'm looking forward to seeing your reply soon. Best regards, Jetflower

    D enhzflepE 2 Replies Last reply
    0
    • T Thang

      Dear everybody, I have a question about the meaning of & operator in the following expression: ((WORD)(((DWORD)(l) >> 16) & 0xFFFF)) 1) What role does the & operator do here ? 2) Why do programmers often use & with hexadecimal, not integer or long integer or double ? I'm looking forward to seeing your reply soon. Best regards, Jetflower

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Thang wrote:

      1. What role does the & operator do here ?

      Bitwise AND.

      Thang wrote:

      1. Why do programmers often use & with hexadecimal, not integer or long integer or double ?

      Bits, bytes, and words are easier to visualize in base-16.

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      1 Reply Last reply
      0
      • T Thang

        Dear everybody, I have a question about the meaning of & operator in the following expression: ((WORD)(((DWORD)(l) >> 16) & 0xFFFF)) 1) What role does the & operator do here ? 2) Why do programmers often use & with hexadecimal, not integer or long integer or double ? I'm looking forward to seeing your reply soon. Best regards, Jetflower

        enhzflepE Offline
        enhzflepE Offline
        enhzflep
        wrote on last edited by
        #3

        Okay, lets say that l = 3131961357 This is the same as 0xBAADF00D Which is the same as 10111010101011011111000000001101 Which means your expression looks a little something like this: ((WORD)(((DWORD)(0xBAADF00D) >> 16) & 0xFFFF)) 3131961357 >> 16 = 47789 0xBAADF00D >> 16 = 0x0000BAAD 10111010101011011111000000001101 >> 16 = 00000000000000001011101010101101 47789 & 65535 = 47789 0x0000BAAD & 0x0000FFFF = 0x0000BAAD 00000000000000001011101010101101 & 00000000000000001111111111111111 = 00000000000000001011101010101101 Basically, the code simply takes a 32 bit number and returns the top (most significant) 16 bits of them. The >> 16 followed by the & 0xFFFF is unnecessary - the bitshift right 16 bits automatically fills the top 16 bits with 0s. This code is functionally the same: (WORD)((DWORD)(l) >> 16) See how much easier things are when we use hex/binary? Edited && --> &

        M T 2 Replies Last reply
        0
        • enhzflepE enhzflep

          Okay, lets say that l = 3131961357 This is the same as 0xBAADF00D Which is the same as 10111010101011011111000000001101 Which means your expression looks a little something like this: ((WORD)(((DWORD)(0xBAADF00D) >> 16) & 0xFFFF)) 3131961357 >> 16 = 47789 0xBAADF00D >> 16 = 0x0000BAAD 10111010101011011111000000001101 >> 16 = 00000000000000001011101010101101 47789 & 65535 = 47789 0x0000BAAD & 0x0000FFFF = 0x0000BAAD 00000000000000001011101010101101 & 00000000000000001111111111111111 = 00000000000000001011101010101101 Basically, the code simply takes a 32 bit number and returns the top (most significant) 16 bits of them. The >> 16 followed by the & 0xFFFF is unnecessary - the bitshift right 16 bits automatically fills the top 16 bits with 0s. This code is functionally the same: (WORD)((DWORD)(l) >> 16) See how much easier things are when we use hex/binary? Edited && --> &

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          We're lucky that

          0XDEADBEEF != 0XBAADFOOD

          This signature was proudly tested on animals.

          enhzflepE 1 Reply Last reply
          0
          • M Maximilien

            We're lucky that

            0XDEADBEEF != 0XBAADFOOD

            This signature was proudly tested on animals.

            enhzflepE Offline
            enhzflepE Offline
            enhzflep
            wrote on last edited by
            #5

            :laugh: :-D

            1 Reply Last reply
            0
            • enhzflepE enhzflep

              Okay, lets say that l = 3131961357 This is the same as 0xBAADF00D Which is the same as 10111010101011011111000000001101 Which means your expression looks a little something like this: ((WORD)(((DWORD)(0xBAADF00D) >> 16) & 0xFFFF)) 3131961357 >> 16 = 47789 0xBAADF00D >> 16 = 0x0000BAAD 10111010101011011111000000001101 >> 16 = 00000000000000001011101010101101 47789 & 65535 = 47789 0x0000BAAD & 0x0000FFFF = 0x0000BAAD 00000000000000001011101010101101 & 00000000000000001111111111111111 = 00000000000000001011101010101101 Basically, the code simply takes a 32 bit number and returns the top (most significant) 16 bits of them. The >> 16 followed by the & 0xFFFF is unnecessary - the bitshift right 16 bits automatically fills the top 16 bits with 0s. This code is functionally the same: (WORD)((DWORD)(l) >> 16) See how much easier things are when we use hex/binary? Edited && --> &

              T Offline
              T Offline
              Thang
              wrote on last edited by
              #6

              Dear Mr enhzflep and Mr DavidCrow, First of all, thank you so much about your clearly explanation. I just still have a consideration, why the small expression (& 0xFFFF) has been still used in the expression ((l) >> 16) & 0xFFFF) here . And this expression origins from window library, hix. Why they still have used this expression if it was not necessary in here ? Once again, thanks for your help, Mr enhzflep and Mr DavidCrow. Best regards, Duong Quoc Thang

              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