My previous post talked about how Objective-C is a thin Smalltalk based layer of object oriented functionality that sits on top of the programming language C. In this post I will revist C in all of its glory. Since C, is at the heart of Objective-C it is probably a good idea to have a good understanding of it before beginning a quest towards mastering Mac development.
C was developed by Dennis Ritchie while working at Bell Labs in 1972. I have sometimes heard C referred to as being a medium level language (if you consider assembly to be low and Java to be high). C was designed to be portable, yet still allow developers to take advantage of all the power found under the hood. The primary use of C is systems programming. It is used extensively for operating systems, hardware control, and embedded systems.
Much of the syntax found in C is replicated in many other languages. Things like methods, parameters, for loops, return types, main function, import statements, etc. can all be found in many other languages like Java. I will glaze over the functionalities of C that can be found in Java and focus on C specific features in the following sections.
Operators
When looking at C code the biggest difference in syntax that I can see are some of the operators that are used. Let's take a look at some operators that may be foreign to a Java programmer. The first set of operators that a Java programmer may not be familiar with are bitwise operations.
Bit shifting
The first operator is the bitwise shift, which comes in two flavors, shift left (<<) and shift right (>>). This operator is used to shift binary numbers left or right. Why would you want to shift binary numbers? Well, shifting binary numbers left or right gives you the best performance possible when you need to divide or multiple by powers of 2. For example, the number four (100), shifted right, will result in 2 (010). 4/2 = 2! If you shift left four (100) would become eight (1000).
Just like we can utilize + or - for assignment (+= / -=), bit shifts can be utilized for assignment as well (<<= / >>=). For example, instead of writing i = i >> 2, we can write i >>= 2. Both of which would result in i being set to the result of 2 right bit shift operations.
One's Complement
In Computer Science, one's complement is one method of representing negative numbers. The most significant bit, determines whether a number is negative or not. This method limits a byte to storing (+/- 127). Without getting into too much more detail, we'll say that the operator (~a), inverts the bits (a boolean NOT operation). For example we will apply the operator to the number 2 (0010). i = ~2; The result of this operation would set i's bits to 1101.
Other Bitwise Boolean Logic Operators: AND (&), OR (|), and XOR (^)
I'm not going to review boolean logic here, but here are a few examples of these operators in use using 5 (101) and 2 (010):
5 & 2 = 0 (000)
5 | 2 = 7 (111)
5 ^ 2 = 7 (111)
The operators can also be used with assignment: &=, |=, ^=
Next time: Pointers
rjett Uncategorized