Close

Code: Printing values in "ones and zeros"

A project log for reprint: modern printf

reprint is printf redone with decades of hindsight, revamping the semantics and syntax to make a function worthy of Hello World.

analog-twoAnalog Two 04/04/2016 at 01:101 Comment

Sometimes fixing a bit twiddling or other low level bug comes down to showing the individual bits. printf does not support printing an integer in radix 2, despite originating from times when code was closer to the metal. The code in reprint is as follows:

/* Print 42 as binary */
reprintf("\f&r", 42);
Octal and hex of course are supported:
/* Print 42 as hex */
reprintf("\f$r", 42);

/* Print 42 as octal */
reprintf("\f%r", 42);

These use of '$', '%', and '&' are not entirely arbitrary, as the three characters are sequential in value:

  1. '$' is 0x24 and selects hexadecimal
  2. '%' is 0x25 and selects octal
  3. '&' is 0x26 and selects radix 2 (binary)
  4. Default output is in decimal

Discussions

Yann Guidon / YGDES wrote 04/04/2016 at 12:12 point

Binary... How many times did I have to write my own routines to process it ? :-)

  Are you sure? yes | no