C语言 C 表示基数为 2 的 int

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6413956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:58:26  来源:igfitidea点击:

C represent int in base 2

cbinaryintegerrepresentation

提问by DevelRoot

Possible Duplicate:
Can I use a binary literal in C or C++?

可能的重复:
我可以在 C 或 C++ 中使用二进制文字吗?

I am learning C and i recently found out that we can represent integers in different ways, like that:

我正在学习 C,最近我发现我们可以用不同的方式表示整数,如下所示:

(Assuming ihas "human-readable" value of 512.) Here are the representations:

(假设i“人类可读”值为512。)以下是表示:

Decimal:

十进制:

int i = 512; 

Octal:

八进制:

int i = 01000;

Hexadecimal:

十六进制:

int i = 0x200;

In base 2 (or binary representation) 512is 1000000000. How to write this in C?

在基数 2(或二进制表示)中5121000000000。如何用C写这个?

Something like int i = 1000000000b? This is funny but unfortunately no C compiler accepts that value.

int i = 1000000000b什么?这很有趣,但不幸的是没有 C 编译器接受这个值。

回答by zneak

The standard describes no way to create "binary literals". However, the latest versions of GCC and Clang support this feature using a syntax similar to the hex syntax, except it's binstead of x:

该标准描述了无法创建“二进制文字”的方法。但是,最新版本的 GCC 和 Clang 使用类似于十六进制语法的语法支持此功能,除了它b不是x

int foo = 0b00100101;

As stated, using such binary literals locks you out of Visual Studio's C and C++ compilers, so you may want to take care where and how you use them.

如上所述,使用此类二进制文字会将您锁定在 Visual Studio 的 C 和 C++ 编译器之外,因此您可能需要注意在何处以及如何使用它们。

C++14 (I know, I know, this isn't C) standardizes support for binary literals.

C++14(我知道,我知道,这不是 C)标准化了对二进制文字的支持。

回答by jason

You can't do this in C.

你不能在 C 中做到这一点。

But you said you are learning C++. In C++ you can use BOOST_BINARYuntil C++0x which will allow user-defined literals.

但是你说你正在学习C++。在 C++ 中,您可以使用BOOST_BINARY直到 C++0x 这将允许用户定义的文字

Please note, however, that it is very easy to become comfortable translating hex to binary and back.

但是请注意,将十六进制转换为二进制并返回非常容易。

For a given binary number, just group the digits in groups of four and learn that

对于给定的二进制数,只需将数字分成四组,然后学习

0000 <-> 0  
0001 <-> 1  
0010 <-> 2   
0011 <-> 3
0100 <-> 4
0101 <-> 5
0110 <-> 6
0111 <-> 7
1000 <-> 8
1001 <-> 9
1010 <-> A
1011 <-> B
1100 <-> C
1101 <-> D
1110 <-> E
1111 <-> F

After a few attempts at doing this translation in your head, you will become very comfortable with it. (Of course, you could do the same with octal, but hex is even more compact than octal.)

在您的头脑中尝试进行几次此翻译后,您会对此感到非常满意。(当然,你可以用八进制做同样的事情,但十六进制比八进制更紧凑。)

For your specific example:

对于您的具体示例:

1000000000 -> 10 0000 0000 -> 0010 0000 0000 -> 0x200

回答by pmg

In C (and I believe C++) there is no binary representation for numbers. However, for the simple number you show, you can use a shortcut

在 C(我相信 C++)中,数字没有二进制表示。但是,对于您显示的简单数字,您可以使用快捷方式

int i = 1 << 9; /* binary 1 followed by 9 binary 0's */

回答by David Heffernan

You can't do this in standard C—the language does not cater for specifying integer literals in binary.

你不能在标准 C 中做到这一点——该语言不适合在二进制中指定整数文字。