1、
#include <stdio.h>
unsigned set(unsigned x, int pos)
{
return x | 1U << pos;
}
unsigned reset(unsigned x, int pos)
{
return x & (~(1U << pos));
}
unsigned inverse(unsigned x, int pos)
{
if(x >> pos & 1U)
return(x & (~(1U << pos)));
else
return(x | 1U << pos);
}
int main(void)
{
unsigned x; int pos;
puts("please input the test number and move bits.");
printf("x = "); scanf("%u", &x);
printf("pos = "); scanf("%d", &pos);
printf("set 1 = %u
", set(x, pos));
printf("set 0 = %u
", reset(x, pos));
printf("set inverse = %u
", inverse(x, pos));
return 0;
}
2、
#include <stdio.h> unsigned set(unsigned x, int pos) { return x | 1U << pos; } unsigned reset(unsigned x, int pos) { return x & ~(1U << pos); } unsigned inverse(unsigned x, int pos) { return x ^ 1U << pos; } int main(void) { unsigned x; int pos; puts("please input the test number and move bits."); printf("x = "); scanf("%u", &x); printf("pos = "); scanf("%d", &pos); printf("set 1 = %u ", set(x, pos)); printf("reset 0 = %u ", reset(x, pos)); printf("inverse = %u ",inverse(x, pos)); return 0; }
3、
#include <stdio.h> unsigned set(unsigned x, int pos) { return x | 1U << pos; } unsigned reset(unsigned x, int pos) { if(x & 1U << pos) return x ^ 1U << pos; else return x; } int inverse(unsigned x, int pos) { return x ^ 1U << pos; } int main(void) { unsigned x; int pos; puts("please input the test number and move bits."); printf("x = "); scanf("%u", &x); printf("pos = "); scanf("%d", &pos); printf("set 1 = %u ", set(x, pos)); printf("reset 0 = %u ", reset(x, pos)); printf("inverse = %u ", inverse(x, pos)); return 0; }