Changes

Jump to: navigation, search

Objective-C Operators and Expressions

2,922 bytes added, 19:45, 7 October 2009
Bitwise Operators
== Bitwise Operators ==
 
In the chapter entitled [[Objective-C 2.0 Data Types]] we talked about the fact that computers work in binary. These are essentially streams of ones and zeros, each one referred to as a bit. Bits are formed into groups of 8 to form bytes. As such, it is not surprising that we, as programmers, will occasionally end up working at this level in our code. To facilitate this requirement, Objective-C provides a range of ''bit operators''. Those familiar with bitwise operators in other languages such as C, C++, C# and Java will find nothing new in the this area of the Objective-C language syntax. For those unfamiliar with binary numbers, now may be a good time to seek out reference materials on the subject in order to understand how ones and zeros are formed into bytes to form numbers. Other authors have done a much better job of describing the subject than we can do within the scope of this book.
 
For the purposes of this exercise we will be working with the binary representation of two numbers. Firstly, the decimal number 171 is represented in binary as:
 
<tt>10101011</tt>
 
Secondly, the number 3 is represented by the following binary sequence:
 
<tt>00000011</tt>
 
Now that we have two binary numbers with which to work, we can begin to look at the Objective-C's bitwise operators:
 
== Bitwise AND ==
 
The Bitwise AND is represented by a single ampersand (&). It makes a bit by bit comparison of two numbers. Any corresponding position in the binary sequence of each number where both bits are 1 results in a 1 appearing in the same position of the resulting number. If either bit position contains a 0 then a zero appears in the result. Taking our two example numbers, this would appear as follows:
 
<tt>10101011</tt> AND<br>
<tt>00000011</tt><br>
<tt>========</tt><br>
<tt>00000011</tt>
 
As we can see, the only locations where both numbers have 1s are the last two positions. If we perform this in Objective-C code, therefore, we should find that the result is 3 (00000011):
 
<pre>
int x = 171;
int y = 3;
int z;
 
z = x & y; // Perform a bitwise AND on the values held by variables x and y
 
NSLog(@"Result is %i", z);
 
2009-10-07 15:38:09.176 t[12919] Result is 3
 
</pre>
 
== Bitwise OR ==
 
The bitwise OR also performs a bit by bit comparison of two binary sequences. Unlike the AND operation, the OR places a 1 in the result if there is a 1 in the first or second operand. The operator is represented by a single vertical bar character (|). Using the our example numbers, the result will be as follows:
 
<tt>10101011</tt> AND<br>
<tt>00000011</tt><br>
<tt>========</tt><br>
<tt>10101011</tt>
 
If we perform this operation in an Objective-C example we see the following:
 
<pre>
int x = 171;
int y = 3;
int z;
 
z = x | y;
 
NSLog(@"Result is %i", z);
 
 
2009-10-07 15:41:39.647 t[13153] Result is 171
</pre>
 
== Bitwise XOR ==

Navigation menu