반응형
C++14의 숫자 리터럴은 이진 형식으로 지정할 수 있다.
구문은 접두사 0b 또는 0B를 사용한다.
구문은 다른 언어에서도 사용됩니다. Java, C#, Swift, Go, Scala, Ruby, Python, OCaml 및 최소 2007년 이후로 일부 C 컴파일러의 비공식 확장으로 사용되었다.
수학적 평가 또는 다양한 타입의 숫자를 포함하는 프로그램을 작성하는 동안 일반적으로 특정 접두사를 사용하여 각 숫자 타입을 지정하는 것을 좋아한다.
다음은 위에서 설명한 것의 예제 프로그램이다.
// C++ program to illustrate the // Hexadecimal and Octal number // using literals #include <iostream> using namespace std; // Driver Code int main() { // Hexadecimal number with // prefix '0x' int h = 0x13ac; // Octal number with prefix '0' int o = 0117; // Print the number of the // hexadecimal form cout << h << endl; // Print the number of the // octal form cout << o; return 0; } //Output : 5036 // 79
이진 리터럴: 16진수 및 8진수와 같은 위의 방식으로 이제 C++14에서 이진 리터럴(0 및 1 형식)을 직접 작성할 수 있다.
이진수는 접두사로 0b또는 0B로 표현할 수 있다.
다음 예제를 살펴보자.
// C++ program to illustrate the // binary number using literals #include <iostream> using namespace std; // Driver Code int main() { // Binary literal with prefix '0b' int a = 0b00001111; cout << a << '\n'; // Binary literal with prefix '0B' int b = 0B00001111; cout << b; return 0; } //Output : 15 // 15
https://www.geeksforgeeks.org/binary-literals-in-c14-with-examples/
https://en.wikipedia.org/wiki/C%2B%2B14#cite_note-9
300x250
'C++ > C++14' 카테고리의 다른 글
C++14 : 제네릭 람다(Generic lambdas) (0) | 2022.04.15 |
---|---|
C++14 : 숫자 구분자(Digit separators) (0) | 2022.04.15 |
C++14 : 애그리게이트 멤버 초기화(Aggregate member initialization) (0) | 2022.04.15 |
C++14 : 변수 템플릿(Variable templates) (0) | 2022.04.15 |
C++14 : 완화된 constexpr 제약 (Relaxed constexpr restrictions) (0) | 2022.04.15 |
댓글