qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [QUESTION] Usage of '0b' as a prefix for numerical constants?


From: Eric Blake
Subject: Re: [QUESTION] Usage of '0b' as a prefix for numerical constants?
Date: Fri, 22 Nov 2019 09:10:46 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.1.1

On 11/22/19 8:56 AM, Aleksandar Markovic wrote:
Hello, all.

I am currently reviewing some code, and I see it uses '0b' as a prefix
of numerical constants, similar to these examples:

switch (((inst >> 22) & 0b1111111000) | ((inst >> 12) & 0b0000000111)) {

or

ARRAY_FIELD_DP32(s->regs, CRB_INTF_ID, RID, 0b0000);

Binary constants introduced by 0b are a gcc'ism, copied by clang, and thus usable in qemu if we want to (similar to our use of the ?: operator, the {} initializer, the ranged case 0 ... 7, __attribute__((cleanup)), ...). But it is not standard C.


I remember a while ago, something stopped me from using '0b' as a
prefix in my own code (was it checkpatch.pl, or perhaps some statement
on coding style, or a compiler, or something else - I don't really
remember), so I didn't use it, and used '0x' (hexadecimal constant).

What is really the view of the community on usage of '0b'?

For small constants, 0b111 is just about as readable as 0x7. But for large constants, I much prefer 0x7f over 0b1111111.


Please C language standard and compiler experts, and also regular
participants like me, speak up.

If you want to provide a patch for coding standards (either admitting that yes we use the extension and here are some guidelines on using it, or declaring no new uses of it and maybe patching existing uses to switch to hex constants), then go for it. Maybe wait for more opinions to come in to see which color more of the developers prefer for their bikeshed.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3226
Virtualization:  qemu.org | libvirt.org




reply via email to

[Prev in Thread] Current Thread [Next in Thread]