c语言 7-2
1、
#include <stdio.h>
#include <math.h>
int main(void)
{
unsigned x;
int n;
puts("please input an unsigned number and an integer.");
printf("x = "); scanf("%u", &x);
printf("n = "); scanf("%d", &n);
(x << n) == x * (int)pow(2, n) ? printf("equally.\n") : printf("unequally.\n");
(x >> n) == x / (int)pow(2, n) ? printf("equally.\n") : printf("unequally.\n");
return 0;
}
2、
#include <stdio.h>
#include <stdio.h>
int main(void)
{
unsigned x;
int n;
puts("please input an unsigned number and an integer.");
printf("x = "); scanf("%u", &x);
printf("n = "); scanf("%d", &n);
int left = x << n;
int leftve = x;
int i;
for(i = 1; i <= n; i++)
{
leftve *= 2;
}
if(left == leftve)
puts("left equally.");
else
puts("left unequally.");
int right = x >> n;
int rightve = x;
for(i = 1; i <= n; i++)
{
rightve /= 2;
}
if(right == rightve)
puts("right equally");
else
puts("right unequally");
return 0;
}
3、
#include <stdio.h>
#include <limits.h>
int main(void)
{
unsigned x;
puts("please input an unsigned integer.");
do
{
printf("x = "); scanf("%u", &x);
if(x > UINT_MAX)
puts("It's too big!");
}
while(x > UINT_MAX);
printf("verify left move 3 unit: %u = %u\n", x << 3, x * 2 * 2 * 2);
printf("verigy right move 3 unit: %u = %u\n", x >> 3, x / 2 / 2 / 2);
return 0;
}
4、
#include <stdio.h>
int main(void)
{
unsigned x;
int n;
puts("please input an unsigned integer and integer.");
printf("x = "); scanf("%u", &x);
printf("n = "); scanf("%d", &n);
int left = x, left_verify = x;
left <<= n;
printf("left = %u\n", left);
int i;
for(i = 1; i <= n; i++)
{
left_verify *= 2;
}
printf("left_vefigy = %u\n", left_verify);
return 0;
}
#include <stdio.h>
int main(void)
{
unsigned x;
int n;
puts("please input an unsigned integer and an integer.");
printf("x = "); scanf("%u", &x);
printf("n = "); scanf("%d", &n);
int right = x, right_verify = x;
right >>= n;
printf("right = %u\n", right);
int i;
for(i = 1; i <= n; i++)
{
right_verify /= 2;
}
printf("right_verify = %u\n", right_verify);
return 0;
}