The downside is that if you ever change the array to be dynamically allocated using new or malloc(), then sizeof() will only return the pointer size. Of course, in a large project you should have have a defined constant for the number of entries.

Else Flattening

Deeply nested else clauses are hard to understand. But if they look like this, you are in luck:

if ( A ) {
} else {
if ( B ) {
} else {
if ( C ) {
}
}
}

Save money on wide screen monitors by flattening the elses.

if ( A ) {
} else if ( B ) {
} else if ( C ) {
}

Black is White and Up is Down if you're not using a mainstream compiler

If your code has to run on a lot of different platforms, you should know that GCC and Microsoft VC++ are very similar and this similarity will trick you into thinking that all compilers are alike. But the C standard leaves a lot of stuff out that will surprise you, if you do work on embedded systems with crappy, but standards-conforming compilers.

What is sizeof(char)? In most compilers it is 1 byte. Switch compilers, and it might be 2 or 4 bytes. The C standards only says that it cannot be larger than an int.

What happens if you add something to the maximum integer?

int a = MAX_INT;
a++;
printf("%dn", a);

In mainstream compilers you get a negative number, since the number space wraps around. But the C standard says the result is undefined. So some compilers will leave a at the maximum value, MAX_INT.

"What is sizeof(char)? In most compilers it is 1 byte. Switch compilers, and it might be 2 or 4 bytes. The C standards only says that it cannot be larger than an int."

This is wrong. On all compilers sizeof(char) is 1. The C standard defines sizeof in terms of numbers of chars, since a char is one byte. If sizeof(char) is not 1, you're not using a conforming C compiler.

rather serious not nice Danger: your flags carry into the next flag if it is set twice. |= would probably be better, (or perhaps a language that doesn't use smileys <= ?:) (or perhaps less than a million settings ;)

> What is sizeof(char)? In most compilers it is 1 byte. Switch compilers, and it might be 2 or 4 bytes. The C standards only says that it cannot be larger than an int.

No, sorry, that is wrong. sizeof(char) is always 1, and the sizeof all other types is measured in chars. Even if a character is stored as 2 or 4 bytes on some platform, sizeof(char) will equal 1. And don't forget that a "byte" is not 8 bits on every architecture.

This list is... just not very good. Parts of it are plain wrong, and others are not as advertised.

On the ternary conditional operator ?: : yes it saves on typing but it's dog-slow compared to a switch statement. Save it for when a switch statement won't work (for instance, when an expression must be reevaluated if it doesn't match). The example you gave is the perfect time to use a switch statement, although your default case should be "str = rand() == 42?"four":"unknown number";break; to make the nested ?: code.

On DeMorgan's: This is CompSci 101, or 201 at the latest. They teach you precisely this in school.

On rounding: this code is not portable to platforms where a sizeof(char) is not 8. For chars the code should be unsigned char bitmap [NUMBER_OF_BITS + sizeof(char)-1/sizeof(char)]; You should be able to figure out how to do that with ints as well.

On real rounding: The best one of the list.

On bit-shifting: if they don't teach you this in school, you're at the wrong school. Same if they don't tell you that if you want multiplication, unless you have hardware reasons to bit-shift (side-effects in a MIPS processor, for instance), you should simply multiply. Let the complier sort it out. The compiler knows better than you, for instance, that multiplication always works, but weird things can happen on certain models of 64-bit processors running in 32-bit mode if you shift left by 32.

Else flattening: seriously dude, what crappy school did you go to that they didn't tell you about this?

On non-mainstream compilers: indeed, so apply that to your own examples! And bit-shifting a negative number works exactly as well as bit shifting a positive number, which is to say, again, if you want multiplication (or division), JUST DO IT!

The nested conditional type is a terrible idea in this circumstance. Instead of letting the compiler generate a O(0) table, you let it generate a O(n) set of if statements. For the non-algorithmic people out there, this means the table lookup always take the same amount of time (and will be much faster than the ifs here), whereas the nested conditionals will keeping checking deeper executed until a match is made.

Adding to the else-if flattening tip: you can make your indentation more consistent by using a if-elif*-else? structure (* and ? used in their regular expression sense). This can be accomplished in C/C++ using a simple macro:

If you install the default "ubuntu-desktop" you also get with it a gigabyte of crap that you will never use. But if you don't install the ubuntu desktop, you get a system with a text-only login: prompt, and it's not clear what to install to get it to a usable state.

I have an irrational need to optimize my Ubuntu installation. I did some investigating and came up with this method, which gives a minimal graphical 1.2 GB install, with gnome, networking, and no applications.

The day arrived when my project was ready to be unleashed upon the world. I waited until the teacher was hovering nearby and then I started my application, running the FORMAT command on the network drive. Some classmates were watching the screen and she hurried over to see what all the fuss was about.

The difference between two sequences A and B can be compactly stored using COPY/INSERT operations. The greedy algorithm for finding these operations relies on an efficient way of finding the longest matching part of A of any given position in B. This article describes how to use a suffix array to find the optimal sequence of operations in time proportional to the length of the input sequences. As a preprocessing step, we find and store the longest match in A for every position in B in two passes over the suffix array.