Home > Articles > Programming > C/C++

What Is C For?

  • Print
  • + Share This
From the author of

Bit Operations

Considering that C is meant to be a low-level language, it’s somewhat depressing that it’s easier to deal with raw binary values in either D or Erlang, both of which are generally regarded as high-level languages. Something as flexible as Erlang’s binary type is far beyond a language like C, but something like D’s bit type would be very helpful.

Look at the source code for any operating system, and you’ll find assembly routines for searching and manipulating bit fields. Adding a bit type and allowing bit arrays would enable a lot of these routines to be moved into the compiler, where they belong. Setting the nth bit in a bit field ought to be as simple as this:

bit foo[128];
foo[n] = 1;

You can do this using a sequence of shift and and operations in pure C, but there’s no guarantee that the compiler will work out what you mean and translate it into something like x86’s BTS instruction.

Similarly, branching on the value of a bit in a word is something that can be done trivially with an architecture like x86, but needs several operations in C. When C is more verbose and less expressive than assembly, something is wrong with C.

  • + Share This
  • 🔖 Save To Your Account