You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Test_C/2.7.txt

34 lines
587 B

#include <stdio.h>
unsigned invert(unsigned x, int p, int n);
void printfbit(unsigned n);
int main()
{
printf("76543210\n\n");
unsigned c = 'f';
printfbit(c);
printfbit(invert(c, 5, 3));
return 0;
}
/* invert: инвертирует n бит из x, начиная с p-й позиции */
unsigned invert(unsigned x, int p, int n)
{
return x ^ (~(~0 << n) << (p+1-n));
}
void printfbit(unsigned n)
{
for(int i = 7; i >= 0; i--)
{
if(n & (1 << i))
putchar('1');
else
putchar('0');
}
putchar('\n');
}