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: Peter Maydell
Subject: Re: [QUESTION] Usage of '0b' as a prefix for numerical constants?
Date: Fri, 22 Nov 2019 16:02:04 +0000

On Fri, 22 Nov 2019 at 14:57, Aleksandar Markovic
<address@hidden> wrote:
> 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'?

I used to be somewhat against it/uncertain, as I wasn't sure how
widely portable it was (as Eric says, it's a gccism, which isn't
inherently a problem for QEMU code but it makes it a
bit less certain in the general case whether all the versions
of gcc and clang we care about have it). But that was some
time ago, and for 0b... we have plenty of existing use in the
tree so we can be confident that it's portable-enough for us.

I agree with Philippe that whether to prefer a hex constant
or a binary one (or a decimal one, for that matter) is basically
a situational question -- aim for whichever seems to make the
intention clear, be most readable, and match up with whatever
notation the official specification uses, if applicable.

PS: for expressions like
 (((inst >> 22) & 0b1111111000) | ((inst >> 12) & 0b0000000111))
it may be preferable to use something like
 (extract32(insn, 25, 7) << 3) | extract32(insn, 12, 3)
rather than raw bitfield manipulation; again this is a
judgement call based on what seems more readable
and perhaps how a specification chooses to phrase it.

thanks
-- PMM



reply via email to

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