background preloader

Hacks/Tips/Anecdotes

Facebook Twitter

C# - Enum "Inheritance" Safe Bitfields in C++ In my cpp11-on-multicore project on GitHub, there’s a class that packs three 10-bit values into a 32-bit integer. I could have implemented it using traditional bitfields… Or with some bit twiddling… uint32_t status = readers | (waitToRead << 10) | (writers << 20); Instead, I did what any overzealous C++ programmer does. I abused the preprocessor and templating system. BEGIN_BITFIELD_TYPE(Status, uint32_t) ADD_BITFIELD_MEMBER(readers, 0, 10) ADD_BITFIELD_MEMBER(waitToRead, 10, 10) ADD_BITFIELD_MEMBER(writers, 20, 10) END_BITFIELD_TYPE() The above set of macros defines a new bitfield type Status with three members. I call this a safe bitfield because it performs safety checks to ensure that every operation on the bitfield fits within the available number of bits. How to Manipulate a Safe Bitfield Let’s take Status as an example. Status status = m_status.load(std::memory_order_relaxed); Setting the value of a bitfield member is easy.

Status.writers = 1023; status.writers = 1024; C++ - Why is "using namespace std;" considered bad practice? Math - How to calculate an RGB colour by specifying an alpha blending amount? The Story of Mel. A recent article devoted to the macho side of programming made the bald and unvarnished statement: Real Programmers write in FORTRAN. Maybe they do now, in this decadent era of Lite beer, hand calculators, and “ user-friendly ” software but back in the Good Old Days, when the term “ software ” sounded funny and Real Computers were made out of drums and vacuum tubes, Real Programmers wrote in machine code.

Not FORTRAN. Not RATFOR. Not, even, assembly language. Machine Code. Lest a whole new generation of programmers grow up in ignorance of this glorious past, I feel duty-bound to describe, as best I can through the generation gap, how a Real Programmer wrote code. I first met Mel when I went to work for Royal McBee Computer Corp., a now-defunct subsidiary of the typewriter company. I had been hired to write a FORTRAN compiler for this new marvel and Mel was my guide to its wonders. . “ If a program can't rewrite its own code ”, he asked, “ what good is it? Mel balked. Bit Twiddling Hacks. By Sean Eron Anderson seander@cs.stanford.edu Individually, the code snippets here are in the public domain (unless otherwise noted) — feel free to use them however you please.

The aggregate collection and descriptions are © 1997-2005 Sean Eron Anderson. The code and descriptions are distributed in the hope that they will be useful, but WITHOUT ANY WARRANTY and without even the implied warranty of merchantability or fitness for a particular purpose. As of May 5, 2005, all the code has been tested thoroughly. Contents About the operation counting methodology When totaling the number of operations for algorithms here, any C operator is counted as one operation. Compute the sign of an integer The last expression above evaluates to sign = v >> 31 for 32-bit integers.

Alternatively, if you prefer the result be either -1 or +1, then use: sign = +1 | (v >> (sizeof(int) * CHAR_BIT - 1)); // if v < 0 then -1, else +1 On the other hand, if you prefer the result be either -1, 0, or +1, then use: Clockwise/Spiral Rule. [This was posted to comp.lang.c by its author, David Anderson, on 1994-05-06.] There is a technique known as the ``Clockwise/Spiral Rule'' which enables any C programmer to parse in their head any C declaration! There are three simple steps to follow: Starting with the unknown element, move in a spiral/clockwise direction; when ecountering the following elements replace them with the corresponding english statements: [X] or [] => Array X size of... or Array undefined size of...

(type1, type2) => function passing type1 and type2 returning... * => pointer(s) to... Keep doing this in a spiral/clockwise direction until all tokens have been covered. Example #1: Simple declaration +-------+ | +-+ | | ^ | | char *str[10]; ^ ^ | | | +---+ | +-----------+ Question we ask ourselves: What is str? ``str is an... We move in a spiral clockwise direction starting with `str' and the first character we see is a `[' so, that means we have an array, so...

Example #2: Pointer to Function declaration ``fp is a...