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