TIP #343: A BINARY SPECIFIER FOR [FORMAT/SCAN] ================================================ Version: $Revision: 1.3 $ Author: Alexandre Ferrieux State: Final Type: Project Tcl-Version: 8.6 Vote: Done Created: Wednesday, 03 December 2008 URL: https://tip.tcl-lang.org343.html Post-History: ------------------------------------------------------------------------- ABSTRACT ========== This TIP proposes to add a %b specifier to the *format* and *scan* commands for working with integers in base-2 representation. BACKGROUND ============ The *format* and *scan* commands already have decimal, hexadecimal, and octal support, but people wanting binary today resort to a hack, namely going hexadecimal first and then *string map*ping the hex digits to their four-bits binary representations. We also already have the "0b" notation as input for *expr*. This lack is inelegant. PROPOSED CHANGE ================= This TIP proposes to reuse the existing binary representation machinery to expose a new *%b* specifier: format %b 5 => 101 scan 000101 %b x;set x => 5 The good properties of the existing code make the addition really painless, in that it automagically combines with the size (l,ll), width (digits), and prefix (#) modifiers: format %#06b 5 => 0b0101 format %llb [expr {(2**72-1)/7}] 1001001001001001001001001001001001001001001001001001001001001001001001 RATIONALE =========== That is really low-hanging fruit. All the pieces are in place, it's just a matter of exposition. The binary representation is a nice pedagogic tool, and having it handy (rather than with a hack) is a bonus. REFERENCE IMPLEMENTATION ========================== See Patch 2368084 []. COPYRIGHT =========== This document has been placed in the public domain. ------------------------------------------------------------------------- TIP AutoGenerator - written by Donal K. Fellows