您可以直接在 c 系列语言中对表示为二进制数字的值进行编码吗?
在基于c(或c-inspired)的语言系列(即C/C++/Objective-C/JavaScript)中,当输入常量时,您可以直接输入该数字的十进制值,也可以在其前面加上前缀0x
通过十六进制写入。 JavaScript 甚至允许您使用八进制编码通过在数字前加上零来键入值(...这对我来说完全是疯狂的,因为 09 看起来是 9 但实际上解析为零!这会让人们感到困惑!应该是 8x
前缀或类似的前缀,但我离题了……)
我想知道是否有一个等效的方法可以直接以二进制方式输入内容。
例如,我很想看到这样的东西:
int x = Bx00001001; // x would equal 9, (bits 8 and 1 are set)
int x = 1x00001001; // Alternate prefix '1x' instead of 'Bx'
只是为了好玩,我实际上正在考虑编写一个预编译器来做到这一点,但同样只是为了好玩,因为它当然该代码不可移植。
尽管如此,还是希望有一个标准的方法来做到这一点。诚然,是的,用十六进制编码非常容易(毕竟你只需要学习最多 15 个),但同样,只是出于好奇而想知道更多。
但说实话……八进制的废话到底是怎么回事!!如果你问我我就疯了!
In the c-based (or c-inspired) family of languages (i.e. C/C++/Objective-C/JavaScript) when typing a constant number, you can simply type the number directly for its decimal value, or you can prefix it with 0x
to write it via hexadecimal. JavaScript lets you even type values using Octal encoding by prefixing the number with a zero (...which is completely bonkers to me since 09 looks to be 9 but actually parses to zero! Way to confuse people! Should've been 8x
prefix or similar, but I digress...)
What I'm wondering is if there's an equivalent for typing things directly in binary.
For instance, I'd love to see something like this:
int x = Bx00001001; // x would equal 9, (bits 8 and 1 are set)
int x = 1x00001001; // Alternate prefix '1x' instead of 'Bx'
Just for fun, I'm actually considering writing a pre-pre-compiler to do exactly that, but again just for fun since it of course the code wouldn't be portable.
Still, hoping there's a standards-way to do this. Granted, yes, it's very easy to just encode in hex (you only need to learn up to 15 after all), but again, just wondering more out of curiosity.
But really... what the heck is up with that Octal nonsense!! Crazy if you ask me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
标准 C 中没有二进制常量。
标准 C 只有十进制(无前缀)、八进制(
0
前缀)和十六进制(0x
或0X
> 前缀)常量。 GNU C 提供二进制常量作为 C 的 GNU 扩展,请参阅:6.60 使用 `0b' 前缀的二进制常量
http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
There is no binary constants in Standard C.
Standard C only has decimal (no prefix), octal (
0
prefix) and hexadecimal (0x
or0X
prefix) constants. GNU C provides binary constants as a GNU extension to C, see:6.60 Binary constants using the `0b' prefix
http://gcc.gnu.org/onlinedocs/gcc/Binary-constants.html
在 C++11 中,您可以通过使用 用户定义的文字< /a> (尽管您必须定义后缀而不是前缀)。
是的,C 和其他 C 派生语言也是如此。据我了解,在18位架构的用户中,八进制比十六进制更受欢迎(因为一个单词恰好是 6 个八进制数字;显然 4.5 个十六进制数字有点尴尬)。但那是在我的时代之前。 ;-)
In C++11, you could accomplish this by using a user-defined literal (though you have to define a suffix rather than a prefix).
Yes, that's also true of C, and other C-derived languages. From what I understand, octal was more popular than hex among users of 18-bit architectures (since a word would be exactly 6 octal digits; obviously 4.5 hex digits is a bit awkward). But that was before my time. ;-)
没有办法只将二进制常量放入代码中。但为什么需要这样做呢?将二进制转换为十六进制很容易。只需将二进制数分成 4 组。每组 4 代表一个十六进制数字。即
0000 -> 0, 0001 --> 1、....1001 --> 9、1010→第1111章 1111 F
There is no way of just putting binary constants into code. But why do you need to? It is easy to convert binary into hex. Just group the binary number into groups of 4. Each group of 4 represents one hex digit. i.e.
0000 -> 0, 0001 -> 1, ....1001 -> 9, 1010 -> A, ... 1111 -> F