cout 二進制輸出


1.cout不支持輸出二進制,只支持八進制、十進制、十六進制輸出,想輸出二進制需要用到bitset

2.每次使用oct、dec、hex之后會將默認輸出形式分別改為八進制、十進制、十六進制,而使用         bitset輸出二進制后則不會改變

 

代碼如下:

 1 #include <iostream>
 2 #include <vector>
 3 #include <ctime>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <bitset>
 7 
 8 int main()  9 { 10     using namespace std; 11 
12     //二進制輸出
13     cout << bitset<sizeof(unsigned long) * 8>(1UL << 27) << endl; 14 
15     cout << (1UL << 27) <<endl; 16 
17     //八進制輸出
18     cout << oct << (1UL << 27) << endl; 19 
20     cout << (1UL << 27) <<endl; 21 
22     //十六進制輸出
23     cout << hex << (1UL << 27) <<endl; 24 
25     cout << (1UL << 27) <<endl; 26 
27     //十進制輸出
28     cout << dec << (1UL << 27) << endl; 29 
30     cout << (1UL << 27) <<endl; 31 
32     return 0; 33 }

結果如下:

e:\C++\C++ primer\4>cd "e:\C++\C++ primer\4\" && g++ test.cc -o test && "e:\C++\C++ primer\4\"test
00001000000000000000000000000000
134217728
1000000000
1000000000
8000000
8000000
134217728
134217728

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM