用移位符》》和《《解决问题 ?
输入一个4字节的变量,拆成4个1字节的变量输出示例
Please Enter 4-byte Value:
0x[提示符]12345678
Result Is:
0x12
0x34
0x56
0x78
使用移位符>>和<<完成这个题目
注意用移位符
root@~ #cat y.c #include <stdio.h> int main (void) { int a,i=24; printf("Enter hex:0x"); scanf("%x",&a); do { printf("0x%x\n",(a>>i&0xff)); i-=8; }while(i>=0); return 0; } root@~ # root@~ #./y Enter hex:0x12345678 0x12 0x34 0x56 0x78 root@~ #./y Enter hex:0x99991234 0x99 0x99 0x12 0x34 root@~ #
#include <stdio.h> #include <stdlib.h> int main(void) { int digit = 32; unsigned int n, index = 0; unsigned int result[4]; printf("Please enter 4-byte value:\n0x"); if(scanf("%x", &n)) while(digit > -1) result[digit -= 8, index++] = n >> digit & 0x000000ff; else exit(1); printf("Result is:\n"); for(index = 0; index < 4; index++) printf("%#x\n", result[index]); return 0; }