标准 C 没有定义二进制常量。不过有一个 GNU C 扩展(在流行的编译器中,clang 也采用了它):0b
or 0B
前缀:
int foo = 0b1010;
如果您想坚持使用标准 C,那么有一个选择:您可以组合宏和函数来创建一个almost可读的“二进制常量”特征:
#define B(x) S_to_binary_(#x)
static inline unsigned long long S_to_binary_(const char *s)
{
unsigned long long i = 0;
while (*s) {
i <<= 1;
i += *s++ - '0';
}
return i;
}
然后你可以像这样使用它:
int foo = B(1010);
如果您打开大量编译器优化,编译器很可能会完全消除函数调用(不断折叠)或至少会内联它,因此这甚至不会成为性能问题。
Proof:
以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#define B(x) S_to_binary_(#x)
static inline unsigned long long S_to_binary_(const char *s)
{
unsigned long long i = 0;
while (*s) {
i <<= 1;
i += *s++ - '0';
}
return i;
}
int main()
{
int foo = B(001100101);
printf("%d\n", foo);
return 0;
}
已使用编译clang -o baz.S baz.c -Wall -O3 -S
,并生成了以下程序集:
.section __TEXT,__text,regular,pure_instructions
.globl _main
.align 4, 0x90
_main: ## @main
.cfi_startproc
## BB#0:
pushq %rbp
Ltmp2:
.cfi_def_cfa_offset 16
Ltmp3:
.cfi_offset %rbp, -16
movq %rsp, %rbp
Ltmp4:
.cfi_def_cfa_register %rbp
leaq L_.str1(%rip), %rdi
movl $101, %esi ## <= This line!
xorb %al, %al
callq _printf
xorl %eax, %eax
popq %rbp
ret
.cfi_endproc
.section __TEXT,__cstring,cstring_literals
L_.str1: ## @.str1
.asciz "%d\n"
.subsections_via_symbols
So clang
完全消除了对函数的调用,并将其返回值替换为101
。整洁吧?