Welcome Guest.

1Answers

C language bit operation

Asked by: Amy Morris 10 views IT May 25, 2023

/*This function is used to switch the rear n bit in a value. To the processing value and n are parameters of the function*/

#include

#include

/ *Turn an integer into string */

CHAR *itobs (int, char *);

/*Display binary numbers*/

void show_bstr (const class*);

/*switch num's post -bits, that is, 1 change 0,0 change 1*/

int Invert_end (int Num, int Bits);

int Main (void)

{

CHAR BIN_STR[Char_bit * sizeof (int) + 1];

int Number;

Puts ("Enter Integers and see the binary");

Puts ("Non-Numeric Input Teminates Program.");

While (scanf ("%d", &number) == 1)p>

itobs (number, bin_str);

Printf ("%d is", number); // The strange place

show_bstr (bin_str);

Putchar ('\ n');

Number = Invert_end (Number, 4);

Putchar ('\ n');

PRINTF ("INVERTING The LAST 4 BITS GIVES \ N");

Itobs (Number, BIN_STR);("%d is", number); // The strange place

Show_bstr (bin_str);

Putchar ('\ n');

}

puts ("bye!");

Return 0;

}

CHAR * itobs (int n, char * ps)

{

int i;

CONST SIZE = char_bit * size (int);

for (i = size -1; i> = 0; i-, n>> 1)

{

ps [i] = (01 &n) + '0';

}

PS [size] = '\ 0';

Return ps;

}

Void Show_BSTR (Const Char * PSTR)

{

int i;

While (pstr [i])

{

Putchar (pstr [i]);

if (++ i % 4 == 0 &&0 &&pstr [i])

{

putchar ('');

}

}notint bitval = 1;

While (bits--> 0)

{

mask | = bitval;

Bitval << = 1;

}

Return num ^ mask;

}

--------In the lower n bit, 1 becomes 0,0 into 1. I test the code that can run normally.For example, input in the picture below 7. My problem is that there are two printf () statements in the main function, printf ("%d was", number); // strange place.If I comment on this statement, then the results cannot be exported correctly after entering 7.I don't understand why.Because I think this is just a statement that outputs the integer I just entered (that is, 7 I entered). As for the output of binary, there is the following show_bstr (bin_str) function.Even if I comment on Printf ("%D IS", Number); then it should also display the subsequent binary number (that is, the content of the show_bstr (bin_str) function).

1 Answers

  1. +3Votes  

    The program has the fault that is slightly linked to Ming Hydrogen

    Your i is not initialized. It comes from the memory change.Remember to stop only the value of the memory of the memory, so that I = 0, so if there is a Printf, you will be able to perform other outputs when you have the PRINTF.It is lucky to continue running.

    Change to int i = 0; that’s it

    Earl- May 25, 2023 |

Answer Question