Unions – ew. As primarily a C++ programmer, I used to shy away from code that contained this kind of black magic. However, unions are actually very cool things and are really not that scary or weird. A union is basically a variable. But instead of being an int, or a float, or a double, [...]
Let’s take a look at the four methods that allow us to utilize dynamic memory in C. Dynamic memory just means we are using memory on the heap, instead of on the stack. Why would you want to use dynamic memory? You might want to create a variable or object that persists beyond the scope [...]
There you are, happily programming away, when suddenly you get a compile error: error: jump to case label error: crosses initialization of ‘int x’ “Huh?” You say, peering at the computer screen. Your code looks fine, so what does it mean? Look closely at your switch statement. A switch statement contains case labels, which provide [...]
Lots of functions in the C standard library will set errno to an error code if something goes wrong, so using errno in your programming can help you pinpoint where problems are occurring and what they might be. errno remains set at the last error code, so bear in mind that: a) if two subsequent [...]
21 September 2011 – 12:51
Let’s take a look at bit shifting in practice. Say we have a variable called colour, that contains an RGBA value. If you have never had any experience with graphics, all you need to know is that the colours you see on your screen may be represented as a combination of four different variables – [...]
14 September 2011 – 12:29
The left and right shift operators are the last two bitwise operators for us to look at. In C (and C++), << and >> are used to shift bits around inside bytes. Not just in any random fashion – these operators move all the bits either to the left or to the right, exactly as [...]
Well, we’re now onto the final of the four bitwise operators – bitwise NOT – probably the most simple of the four to understand. Bitwise NOT is represented by the tilde symbol, ~, and only takes one operand. Can I see an example? ~125 = 130 What is this actually doing? Bitwise NOT essentially flips [...]
Today we’ll look at the third out of four bitwise operators – bitwise XOR. Bitwise XOR is represented by the symbol ^ and is an exclusive OR operation. Can I see an example? 130 ^ 10 = 136 What is this actually doing? Bitwise XOR compares each bit setting and if either one is set, [...]
Following on from last week’s look at the bitwise AND operator, this week I’m going to look at bitwise OR and provide an example of how you can use it. Bitwise OR is represented by the | symbol. Can I see an example? 130 | 10 = 138 What is this actually doing? Bitwise OR [...]
I’m going to spend the next few weeks taking a look at the bitwise operators (AND, NOT, OR, XOR), and how they are used in C/C++. How often you are faced with problems that use these operators depends on the field you are working in, and the coding preferences of the people that have gone [...]