PDA

View Full Version : Learning Objective-C - Are bitwise operators important?




Erix18
Apr 17, 2009, 10:48 AM
I was completely confused and I do not understand the application or how you would use it, I kind of get the basic principle though. As someone who wants to write GUI programs for mac and iPhone, is this critical to my education (if so I will spend more time trying to understand it...)?



lee1210
Apr 17, 2009, 10:52 AM
It is unlikely that you will need it day to day. If you never read or use anyone else's code that uses them, and you don't know how to use them yourself, you'll never write any code that uses them, so you're fine. But if you ever use libraries and they use some constants that get masked together for options or something you should at least have a cursory understanding of | and &.

-Lee

kainjow
Apr 17, 2009, 12:23 PM
It's actually used fairly often. For example [NSString rangeOfString:options:] or [NSTrackingArea initWithRect:options:owner:userInfo:]. Both of these methods' option argument takes an OR'd set of values.

garethlewis2
Apr 17, 2009, 12:31 PM
Need to put the pedantic hat on.

Those two class methods use predefined bit masks. Most developers will just see them used in an example and never deviate from them, or bother to understand what they do.

Pedantic mode off.

brian6504
Apr 17, 2009, 12:53 PM
Need to put the pedantic hat on.

Those two class methods use predefined bit masks. Most developers will just see them used in an example and never deviate from them, or bother to understand what they do.

Pedantic mode off.

Understanding what your code does is not pedantic at all. There are far more than two methods that require the use of bitwise or-ed flags and it can be useful in other scenarios. If you don't fully understand what the code you are writing mean you shouldn't be shipping it at all. Just because someone else does it doesn't make it right.

It looks like many iPhone devs thought understanding what their code did was also pedantic, which explains why most of the software for that platform is utter crap.

GorillaPaws
Apr 17, 2009, 01:22 PM
If you don't understand this concept you'll be ok for a while imo. You will probably need to learn it down the road, but don't let it stop you from moving on in your text/tutorial if you're having issues with it.

lee1210
Apr 17, 2009, 02:06 PM
I guess i would say that it does come up, you will run into it, so why not learn it now? I wouldn't want it to totally flummox you and put you off programming for good, but it's certainly not the hardest concept I've run into, so it may be worth working through it now.

The real difficulty is that to know what's going on you need to have an understanding of binary numbers. I'll avoid floating point for now, as it's unnecessary and i rarely see bitwise operators used against floats or doubles.
The integer types in C/objective-C are stored in 2's complement. Reading up on this may come in handy, later, too:
http://en.wikipedia.org/wiki/Two's_complement

Most basically, binary is a number system like base 10/decimal that you are used to dealing with. Instead of 10 possible values for each place/-it (digit in decimal), there are only 2. Each place is called a bit (bi- for two, dig- for ten). Instead of a 1s place, 10s place, 100s place like in decimal, there is a 1s place, a 2s place, a 4s place, an 8s place, etc. Since there are only two possible values for each bit, each place is either "on" (1) or "off" (0). For a positive number, it's pretty easy to just look at a value and convert it to base 10. For example:
0101
8's place is 0. +0
4's place is 1. +4
2's place is 0. +0
1's place is 1. +1
Decimal: 5

There's no 4-bit datatype in C, but i'll stick to that width for simplicities sake. The bitwise or, |, checks the bits at each place and if either is 1, the result is 1. It's table looks like this:
_| 0 | 1 |
0| 0 | 1 |
1| 1 | 1 |

To read the table, just pick one of your bits on the top of the table, and one from the left side, and look in the square they meet. This will be the result for that bit. So:
0010 | 1010 = 1010
1000 | 1011 = 1011
0101 | 1010 = 1111

Each bit is computed separately (hence the term bit-wise).

Here is the table for and, &:
_| 0 | 1 |
0| 0 | 0 |
1| 0 | 1 |

0010 & 1010 = 0010
1000 & 1011 = 1000
0101 & 1010 = 0000

And here is the table for exclusive or, ^,which means exactly one of the bits being compared is 1. This is, in my opinion, the best operator.
_| 0 | 1 |
0| 0 | 1 |
1| 1 | 0 |

0010 ^ 1010 = 1000
1000 ^ 1011 = 0011
0101 ^ 1010 = 1111

Generally these operations are used to store "a lot" of options in a small space. For example, in a short int you have 16 bits available, so you can represent 16 different options being "on" or "off". In an int you have 32 such options. Normally there will be things #define'd, etc. that are set to a constant with one such bit set for each option, and perhaps another set to turn an option off, with all bits but that set. I'll keep using binary and psuedocode:

#define optAon 0001
#define optBon 0010
#define optCon 0100
#define optDon 1000
#define optAoff 1110
#define optBoff 1101
#define optCoff 1011
#define optDoff 0111

//...
x=x|optAon; //x is now the same as before, but the bit defined for option A is set, whether or not it was before
//..
x=x&optBoff; //x is now the same, but with option B's bit off, whether or not it was before
//..
x=optAon|optBon; //x now has option A and option B set, and C and D off, regardless of its previous value


Code like this is often used to modify options for a function, etc. when there are a small set of "on/off" options. Some have been mentioned above.

There are certainly more exotic things that can be done with these operators and the bitshift operators << and >>, but hopefully this helps a bit with where you are now.

-Lee