(Reporter) How can we improve 10 to 2? (C/C ++) (C) (STL)

Source: Internet
Author: User
Tags bitset

Abstract
Printf () can only display the value of the 10, 8, and 16 bits, but cannot display the value of the 2 bits, but sometimes we want to directly display the 2-in-number.

Introduction
Use environment: Visual C ++ 8.0/Visual Studio 2005

Method 1:
This is changed from C primer plus 5/E. Bit operations are used to bring 10 in two places, which is a pretty good method.

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 }

Results

8   =   00000000000000000000000000001000

16 rows

PS [I -- ] = ( 1   & N) +   ' 0 ' ;

Perform mask on N and retrieve 0th bits. This sort method is faster than % 2. Because it is an int, it is converted into char through + '0, the delimiter column of the stored string.

17 rows

N >>=   1 ;

0 bit processing, processing 1 bit.

15 rows

While (I + 1 ){

Equivalent to while (I> = 0)

20 rows

PS [size] =   ' \ 0 ' ;

End the string.

Method 2:
If you can use C ++, there will be a very simple solution.

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 }

Results

8   =   00000000000000000000000000001000

After bitset is used, cout comes out as 2.

Conclusion
Another method is to use % 2 in combination with memory loss. However, since the bit loss method is fast and saves the attention, there is not much discussion.

Reference
Stephen Prata, C primer plus 5/E

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.