Digital mutual conversion source program 1

zhaozj2021-02-16  75

Transition from decimal number to binary

Servers: Winfox Published in: 2004-3-24 Transition from Decimal Number to Binary Ratio // By Lynxcn Version 1.0 // Reprinted Please keep the integrity of the article // This article only studies the unsigned integer into a binary number one, Algorithm 1. In addition to the decimal integersion of 2, and record the remainder, until the business is 0, and finally read the remainder. Such as: convert the decimal number 10 into a corresponding binary count 10/2 5 05/2 2 12/2 1 01/2 0 1 reverse readout is: 1010 2. Bit operation Since the data is in the computer In binary form, we only need to output it (the problem is here). Bit and Operation (&): Participate in two data, "and" operation according to the binary position. If the two corresponding binary locations are 1, the result is 1, otherwise 0. Such as: 0 & 0 = 0, 0 & 1 = 0, 1 & 0 = 0, 1 & 1 = 1 decimal number 3,13 bits and below: 0 0 0 0 0 0 1 1 (3) & 0 0 0 0 1 1 0 1 (13) ------------------------------ 0 0 0 0 0 0 0 1 (1) Mask: Mask is some The bit is set to open (1), and some bits are set to the combination of bit (0). Suppose the symbol constant Mask is 1000, we perform bit and (&) operation: index = index & mask; This statement sets index corresponding to the bit bit to 1, other position 0. Because: 0 & 1 = 0, 1 & 1 = 1, ie For itself; regardless of 0 or 1 and 0 bit and the result is 0, that is, the screen is Second, the program list *************************************************************** *********** List 1: Circulation ***************************************** *********************** // Cody by lynxcn // decimal ---> binary #include

#define M (SIZEOF (Int)> 2? 32: 16)

Int main (void)

{

INT P, INDEX = 0, BIN [M] = {0};

Int Num;

Cout << "Enter a Num:";

NUM;

IF (Num <0)

COUT << "Data Error" <

Else IF (NUM == 0)

Cout << 0 <

Else

{

DO {// algorithm

BIN [INDEX] = Num% 2;

Num = NUM ​​/ 2;

INDEX ;

WHILE (NUM);

// filter out the preamble is invalid 0

Index = m-1;

While (! bin [index])

{

Index -;

}

P = index;

// output

For (INDEX = P; INDEX> = 0; index -)

Cout <

Cout <

}

Return 0;

}

*********************************************************** ******** Listing 2: Recursive function (but efficiency is not very high) ************************************* ************************************* // Cody By Lynxcn // Decondition ---> Binary #include

Void Dec_bin (int);

INT main (void) {

Int Num;

Cout << "Enter a Num:";

NUM;

IF (Num <0)

COUT << "Data Error" <

Else

DEC_BIN (NUM);

Cout <

Return 0;

}

Void Dec_bin (int N)

{

Int mod;

MOD = N% 2;

IF (n> = 2)

Dec_bin (n / 2); // Because the remainder of the first request is to output,

Cout <

Return;

}

*********************************************************** ******** List 3: Bit operation ********************************************* ********************* // Cody by Lynxcn // Decoction ---> Binary #include

Int main (void)

{

Unsigned Num, Mask;

Mask = sizeof (int)> 2? 0x800000: 0x8000;

COUT << "Enter a number:";

NUM;

IF (NUM == 0)

Cout << 0 <

Else

{

While (Mask)

{

IF (NUM & MASK) // Filter preamble is invalid 0

{

While (mask) // algorithm

{

COUT << (NUM & MASK? 1: 0);

Mask >> = 1;

}

Cout <

Break;

}

Mask >> = 1;

}

}

Return 0;

}

Win2k Server VC6 is debugged; three, other binary will be converted, octal, and hexadecimal is naturally not going on. :) end. 03-24-2004