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.6.txt

38 lines
738 B

#include <stdio.h>
unsigned setbits(unsigned x, int p, int n, unsigned y);
void printfbit(unsigned n);
int main()
{
printf("76543210\n\n");
unsigned c1 = 'f';
printfbit(c1);
unsigned c2 = 'z';
printfbit(c2);
printfbit(setbits(c1, 5, 3, c2));
return 0;
}
/* setbits: x получает n правых бит из y, начиная с p-й позиции */
unsigned setbits(unsigned x, int p, int n, unsigned y)
{
return ( (~0 << (p+1)) & x ) | ( ~(~0 << (p+1-n)) & x ) |
(~(~0 << n) & y) << (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');
}