(筆記) 如何將10進位轉2進位? (C/C++) (C) (STL)
2008-07-22 16:15
351 查看
Abstract
printf()只能顯示10、8、16進位的值,卻無法顯示2進位的值,但有時候我們會希望能直接顯示2進位數字。
Introduction
使用環境:Visual C++ 8.0 / Visual Studio 2005
Method 1:
這是從C Primer Plus 5/e改寫的,使用bit運算來將10進位轉2進位,相當漂亮的寫法。
decimal2binary.c / C
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : decimal2binary.c
5 Compiler : Visual C++ 8.0
6 Description : Demo how to convert deciaml to binary by C
7 Release : 07/22/2008 1.0
8 */
9 #include <stdio.h>
10
11 char* itobs(int n, char *ps) {
12 int size = 8 * sizeof(n);
13 int i = size -1;
14
15 while(i+1) {
16 ps[i--] = (1 & n) + '0';
17 n >>= 1;
18 }
19
20 ps[size] = '\0';
21 return ps;
22 }
23
24 int main() {
25 int n = 8;
26 char s[8 * sizeof(n) + 1];
27
28 printf("%d = %s\n", n, itobs(n,s));
29 }
執行結果
8 = 00000000000000000000000000001000
16行
ps[i--] = (1 & n) + '0';
對n做mask,將第0 bit取出來,這樣的寫法比%2快,因為取出是個int,所以透過 + '0'轉成char,存入字串陣列。
17行
n >>= 1;
0 bit處理完,處理1 bit...以此類推。
15行
while(i+1) {
相當於while(i >=0)
20行
ps[size] = '\0';
將字串收尾。
Method 2:
若能用C++,就有很簡單的解法。
decimal2binary.cpp / C++
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : decimal2binary.cpp
5 Compiler : Visual C++ 8.0
6 Description : Demo how to convert deciaml to binary by C++
7 Release : 07/22/2008 1.0
8 */
9 #include <iostream>
10 #include <bitset>
11
12 using namespace std;
13
14 int main() {
15 int n = 8;
16 bitset<sizeof(n) * 8> s(n);
17
18 cout << n << " = " << s << endl;
19 }
執行結果
8 = 00000000000000000000000000001000
使用bitset後,cout出來就是2進位了。
Conclusion
另外一種寫法就是用%2搭配遞迴,不過既然bit寫法速度快又省記憶體,就沒多討論了。
Reference
Stephen Prata,C Primer Plus 5/e
printf()只能顯示10、8、16進位的值,卻無法顯示2進位的值,但有時候我們會希望能直接顯示2進位數字。
Introduction
使用環境:Visual C++ 8.0 / Visual Studio 2005
Method 1:
這是從C Primer Plus 5/e改寫的,使用bit運算來將10進位轉2進位,相當漂亮的寫法。
decimal2binary.c / C
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : decimal2binary.c
5 Compiler : Visual C++ 8.0
6 Description : Demo how to convert deciaml to binary by C
7 Release : 07/22/2008 1.0
8 */
9 #include <stdio.h>
10
11 char* itobs(int n, char *ps) {
12 int size = 8 * sizeof(n);
13 int i = size -1;
14
15 while(i+1) {
16 ps[i--] = (1 & n) + '0';
17 n >>= 1;
18 }
19
20 ps[size] = '\0';
21 return ps;
22 }
23
24 int main() {
25 int n = 8;
26 char s[8 * sizeof(n) + 1];
27
28 printf("%d = %s\n", n, itobs(n,s));
29 }
執行結果
8 = 00000000000000000000000000001000
16行
ps[i--] = (1 & n) + '0';
對n做mask,將第0 bit取出來,這樣的寫法比%2快,因為取出是個int,所以透過 + '0'轉成char,存入字串陣列。
17行
n >>= 1;
0 bit處理完,處理1 bit...以此類推。
15行
while(i+1) {
相當於while(i >=0)
20行
ps[size] = '\0';
將字串收尾。
Method 2:
若能用C++,就有很簡單的解法。
decimal2binary.cpp / C++
1 /*
2 (C) OOMusou 2008 http://oomusou.cnblogs.com
3
4 Filename : decimal2binary.cpp
5 Compiler : Visual C++ 8.0
6 Description : Demo how to convert deciaml to binary by C++
7 Release : 07/22/2008 1.0
8 */
9 #include <iostream>
10 #include <bitset>
11
12 using namespace std;
13
14 int main() {
15 int n = 8;
16 bitset<sizeof(n) * 8> s(n);
17
18 cout << n << " = " << s << endl;
19 }
執行結果
8 = 00000000000000000000000000001000
使用bitset後,cout出來就是2進位了。
Conclusion
另外一種寫法就是用%2搭配遞迴,不過既然bit寫法速度快又省記憶體,就沒多討論了。
Reference
Stephen Prata,C Primer Plus 5/e
相关文章推荐
- (转) 如何將10進位轉2進位? (C/C++) (C)
- (筆記) 如何判斷回文(palindrome) ? (C/C++) (C) (STL)
- (筆記) 如何在字串中從指定字元抓到指定字元? (C/C++) (C)
- iphone开发之C++和Objective-C混编 如何在xcode中用C++的STL
- 新手如何学STL
- 如何列出陣列中大於n的所有元素? (C/C++) (STL)
- 如何將container中的iterator,從一個值取代成另外一個值? (C/C++) (STL)
- (筆記) 如何使用C#使用Win32 DLL? (.NET) (C#) (Windows Form)
- (筆記) 如何對一變數指定某一個bit的值? (SOC) (C/C++) (Verilog)
- (原創) 如何将字符串前后的空白去除? (C/C++) (STL)
- (原創) 如何為程式碼加上行號? (C/C++) (STL)
- 如何导出标准模板库(STL)类的实例化和包含STL类对象数据成员的类
- STL之如何选择顺序容器
- 【C++ STL应用与实现】64: 如何使用shuffle和random_shuffle : 洗牌 (since C++11)
- 【C++ STL应用与实现】18: 如何使用迭代器适配器
- (筆記) 如何使UltraEdit支援Verilog語法顯示? (SOC) (Verilog) (UltraEdit)
- (筆記) 如何得知Quartus II最新版到底修改了哪些東西? (SOC) (Quartus II) (Nios II) (SOPC Builder) (Qsys)
- C++ - 部分STL容器如何去除重复元素
- 【C++ STL应用与实现】72: 标准库里的堆--如何使用标准库的heap算法
- (筆記) 如何將memory轉成vector? (SOC) (Verilog)