Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Bitwise Operators and masks

Bitwise Operators and masks
Thread Tools
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jul 13, 2002, 09:06 AM
 
Hello all,

Sorry to ask such a simple question, but I just want to be absolutely sure I've grokked this concept properly:

I want to get and set various bits (flags) in a long using masks defined in internetConfig.h (see below

</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">
typedef long ICMapEntryFlags;
typedef short ICFixedLength;

#if !OLDROUTINENAMES
struct ICMapEntry {
short totalLength;
ICFixedLength fixedLength;
short version;
OSType fileType;
OSType fileCreator;
OSType postCreator;
ICMapEntryFlags flags;

Str255 extension;
Str255 creatorAppName;
Str255 postAppName;
Str255 MIMEType;
Str255 entryName;

....

enum {
kICMapBinaryBit = 0, /* file should be transfered in binary as opposed to text mode*/
kICMapResourceForkBit = 1, /* the resource fork of the file is significant*/
kICMapDataForkBit = 2, /* the data fork of the file is significant*/
kICMapPostBit = 3, /* post process using post fields*/
kICMapNotIncomingBit = 4, /* ignore this mapping for incoming files*/
kICMapNotOutgoingBit = 5 /* ignore this mapping for outgoing files*/
};

enum {
kICMapBinaryMask = 0x00000001, /* file should be transfered in binary as opposed to text mode*/
kICMapResourceForkMask = 0x00000002, /* the resource fork of the file is significant*/
kICMapDataForkMask = 0x00000004, /* the data fork of the file is significant*/
kICMapPostMask = 0x00000008, /* post process using post fields*/
kICMapNotIncomingMask = 0x00000010, /* ignore this mapping for incoming files*/
kICMapNotOutgoingMask = 0x00000020 /* ignore this mapping for outgoing files*/
};

};</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">I have read the following link:

<a href="http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=bit+mask&action=Search" target="_blank">http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=bit+mask&action=Search</a>

which lead me to write the following code:
("entry" is an instance variable.)
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">
-(BOOL)shouldIgnoreMappingForOutgoingFiles
{
ICMapEntryFlags flags;
BOOL ignore;

flags=entry.flags;
ignore=(flags &amp; kICMapNotOutgoingMask);
return ignore;
}

-(void)setShouldIgnoreMappingForOutgoingFilesBOOL )yn
{
ICMapEntryFlags flags;
if (yn) {
flags=(entry.flags | kICMapNotOutgoingMask);
} else {
flags=(entry.flags ^ kICMapNotOutgoingMask);
}
entry.flags=flags;
}</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">If I want to alter "flags" so that just the "not outgoing" bit is set to YES then I use bitwise OR.

If I want to alter "flags" so that just the "not outgoing" bit is set to NO then I use bitwise Exclusive OR.

Is this right? The reading part seems to work, but the setting has some very strange behaviour.

<small>[ 07-13-2002, 10:08 AM: Message edited by: Diggory Laycock ]</small>
     
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status: Offline
Reply With Quote
Jul 13, 2002, 10:44 AM
 
The ^ symbol is an XOR operator, definitely not what you want. What you want to do is flags = (entry.flags & (~kICMapNotOutgoingMask)). The ~ inverts the mask so that only the bit you want to clear is 0 and the rest are 1, then the & will not affect any of the other bits but is guaranteed to set the one you want to clear to 0.
     
Professional Poster
Join Date: Oct 2001
Location: London
Status: Offline
Reply With Quote
Jul 13, 2002, 11:10 AM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by bewebste:
<strong>The ^ symbol is an XOR operator, definitely not what you want. What you want to do is flags = (entry.flags & (~kICMapNotOutgoingMask)). The ~ inverts the mask so that only the bit you want to clear is 0 and the rest are 1, then the & will not affect any of the other bits but is guaranteed to set the one you want to clear to 0.</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Thanks, most appreciated - Hope someday I can repay the compliment (geddit ) <img border="0" title="" alt="[Wink]" src="wink.gif" />
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 10:02 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2