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. bitwise operations

bitwise operations

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 5 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.
  • A Offline
    A Offline
    alex barylski
    wrote on last edited by
    #1

    I have a number like 400 I have another number like 666 Is there anyway using bitwise operators I can extract ONLY the bits which are used to make 400 and determine if all those bits are set??? I have read PJ Arends bitwise article numerous times, but still I am missing something and I need this for a solution... Basically MASK only certin bits and determine if all are set??? Is this possible? Can you show me how please??? :) It's frustrating being a genius and living the life of a moron!!!

    D 1 Reply Last reply
    0
    • A alex barylski

      I have a number like 400 I have another number like 666 Is there anyway using bitwise operators I can extract ONLY the bits which are used to make 400 and determine if all those bits are set??? I have read PJ Arends bitwise article numerous times, but still I am missing something and I need this for a solution... Basically MASK only certin bits and determine if all are set??? Is this possible? Can you show me how please??? :) It's frustrating being a genius and living the life of a moron!!!

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

      Hockey wrote:

      Is there anyway using bitwise operators I can extract ONLY the bits which are used to make 400...

      if ((666 & 400) == 400) // will evaluate to 0
      ...

      Hockey wrote:

      Basically...determine if all are set???

      This makes no sense. If all of the bits of a number are set, then the number is 'fully intact' (i.e., there's nothing to test).


      "Take only what you need and leave the land as you found it." - Native American Proverb

      A 1 Reply Last reply
      0
      • D David Crow

        Hockey wrote:

        Is there anyway using bitwise operators I can extract ONLY the bits which are used to make 400...

        if ((666 & 400) == 400) // will evaluate to 0
        ...

        Hockey wrote:

        Basically...determine if all are set???

        This makes no sense. If all of the bits of a number are set, then the number is 'fully intact' (i.e., there's nothing to test).


        "Take only what you need and leave the land as you found it." - Native American Proverb

        A Offline
        A Offline
        alex barylski
        wrote on last edited by
        #3

        Basically whats happening here is I am trying to determine which checkboxes in an FTP program should be checked... Based on the linux permissions of the file... So, For example: 777 would require all the checkboxes RWX for Owner/Group/Other to be checked... Whereas 400, would require ONLY the READ/Owner to be checked... I have the permission bits, ie: 777 or 666 or 400... Now i'm trying to use this number and programatically determine which of the corresponding checkboxes need to be checked... I cannot use any FTP helper functions, etc...I must do this completely independant of any library... The reason I asked if it's possible to localize a certain number inside another...is because... 400 in binary = 110010000 - 3 bits are SET, but ONLY one checkbox (READ/Owner is checked) However, 666 in binary = 1010011010 - 5 bits are SET and all READ/WRITE for Owner, Group, Other are checked!!! A trivial: ((666 & 400) == 400) Doesn't work!!! I'm not sure why it doesn't work...but it doesn't work...atleast the last time I tried it it didn't work :) Now does it make sense what I am trying to accomplish??? Any ideas??? It's frustrating being a genius and living the life of a moron!!!

        K M 2 Replies Last reply
        0
        • A alex barylski

          Basically whats happening here is I am trying to determine which checkboxes in an FTP program should be checked... Based on the linux permissions of the file... So, For example: 777 would require all the checkboxes RWX for Owner/Group/Other to be checked... Whereas 400, would require ONLY the READ/Owner to be checked... I have the permission bits, ie: 777 or 666 or 400... Now i'm trying to use this number and programatically determine which of the corresponding checkboxes need to be checked... I cannot use any FTP helper functions, etc...I must do this completely independant of any library... The reason I asked if it's possible to localize a certain number inside another...is because... 400 in binary = 110010000 - 3 bits are SET, but ONLY one checkbox (READ/Owner is checked) However, 666 in binary = 1010011010 - 5 bits are SET and all READ/WRITE for Owner, Group, Other are checked!!! A trivial: ((666 & 400) == 400) Doesn't work!!! I'm not sure why it doesn't work...but it doesn't work...atleast the last time I tried it it didn't work :) Now does it make sense what I am trying to accomplish??? Any ideas??? It's frustrating being a genius and living the life of a moron!!!

          K Offline
          K Offline
          kevincwong
          wrote on last edited by
          #4

          666 => 1010011010 400 => 0110010000 bitwise & => 0010010000 It probably will not match 400 ( 0110010000 ). In Unix, those permission are in octet. So, you may want to try ((0666 & 0400 ) == 0400 ) If I understand your problem correctly, I probably will try different approach. You probably have 9 checkbox for all permissions. I probably will have a function to take the permission & all 9 checkbox. Inside the function. if ( permission & 0400 ) ownerRead->SetCheck ( TRUE); if ( permission & 0200 ) ownerWrite->SetCheck ( TRUE); & etc. Kevin

          1 Reply Last reply
          0
          • A alex barylski

            Basically whats happening here is I am trying to determine which checkboxes in an FTP program should be checked... Based on the linux permissions of the file... So, For example: 777 would require all the checkboxes RWX for Owner/Group/Other to be checked... Whereas 400, would require ONLY the READ/Owner to be checked... I have the permission bits, ie: 777 or 666 or 400... Now i'm trying to use this number and programatically determine which of the corresponding checkboxes need to be checked... I cannot use any FTP helper functions, etc...I must do this completely independant of any library... The reason I asked if it's possible to localize a certain number inside another...is because... 400 in binary = 110010000 - 3 bits are SET, but ONLY one checkbox (READ/Owner is checked) However, 666 in binary = 1010011010 - 5 bits are SET and all READ/WRITE for Owner, Group, Other are checked!!! A trivial: ((666 & 400) == 400) Doesn't work!!! I'm not sure why it doesn't work...but it doesn't work...atleast the last time I tried it it didn't work :) Now does it make sense what I am trying to accomplish??? Any ideas??? It's frustrating being a genius and living the life of a moron!!!

            M Offline
            M Offline
            markkuk
            wrote on last edited by
            #5

            You're missing the fact that the Unix numerical permissions are octal (base 8) numbers. This means 400 octal is 100000000 in binary and 666 octal is 101101101 in binary.

            J D 2 Replies Last reply
            0
            • M markkuk

              You're missing the fact that the Unix numerical permissions are octal (base 8) numbers. This means 400 octal is 100000000 in binary and 666 octal is 101101101 in binary.

              J Offline
              J Offline
              Jorgen Sigvardsson
              wrote on last edited by
              #6

              Indeed. Each digit in an octal number, is represented by three bits in the binary number system. E.g: base 8: xyz => base 2: x0x1x2y0y1y2z0z1z2 -- Pictures[^] from my Japan trip.

              1 Reply Last reply
              0
              • M markkuk

                You're missing the fact that the Unix numerical permissions are octal (base 8) numbers. This means 400 octal is 100000000 in binary and 666 octal is 101101101 in binary.

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

                markkuk wrote:

                ...and 666 octal is 101101101 in binary.

                Correction. 666 in base 8 is 110110110 in base 2.


                "Take only what you need and leave the land as you found it." - Native American Proverb

                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