[PATCH] printf: Add %B convespecifier for binary

Alejandro Colomar colomar.6.4.3@gmail.com
Wed Oct 20 16:57:44 GMT 2021


Hi Florian (and other glibc programmers),

On 4/27/20 4:46 PM, Florian Weimer wrote:
> * Alejandro Colomar via Libc-alpha:
> 
>> This patch adds a new feature to the ``printf`` family of functions:
>>
>> ``%B`` conversion specifier for printing unsigned numbers in binary.
>>
>> Behaviour is exactly as with ``%X``, only changing the base (16 -> 2).
>>
>> As Jonathan Nieder pointed out in a previous email, ``%b`` is already in
>> use by ``printf(1)``, so I didn't use it for binary.
> 
> Do any other libcs already have a such a feature?

No, AFAIK.
> 
> In the past, we ran into problems when ISO C standardized specifies we
> already used for something else.  I do not know if there are any plans
> to add binary literals to C (to match C++).  If they were added, maybe
> it would make sense to add the corresponding printf format specifier
> at the same time.

C2x has added binary literals.

<http://open-std.org/jtc1/sc22/wg14/www/docs/n2596.pdf>

6.4.4.1    Integer constants
Syntax
1	integer-constant:
		decimal-constant  integer-suffixopt
		octal-constant  integer-suffixopt
		hexadecimal-constant  integer-suffixopt
		binary-constant  integer-suffixopt
	decimal-constant:
		nonzero-digit
		decimal-constant  digit
	octal-constant:
		0
		octal-constant  octal-digit
	hexadecimal-constant:
		hexadecimal-prefix  hexadecimal-digit
		hexadecimal-constant  hexadecimal-digit
	hexadecimal-prefix: one of
		0x 0X
	binary-prefix: one of
		0b 0B
	nonzero-digit: one of
		1 2 3 4 5 6 7 8 9
	octal-digit: one of
		0 1 2 3 4 5 6 7
	hexadecimal-digit: one of
		0 1 2 3 4 5 6 7 8 9
		a b c d e f
		A B C D E F
	binary-digit: one of
		0 1

> 
> To be honest, due to the present level of C standards activity, I
> think it is very hard to extend ISO C functions at this time, I'm afraid.
> 

Any changes in the acceptance (or actually non-acceptance) after C2x 
having added binary literals?

Thanks,

Alex

-- 
Alejandro Colomar
Linux man-pages comaintainer; https://www.kernel.org/doc/man-pages/
http://www.alejandro-colomar.es/


More information about the Libc-alpha mailing list