用指针实现高低位倒序
扫描二维码
随时随地手机看文章
昨晚在微信群看到一个读者发的面试题目,从网上截图出来的,我百思不得其解,题目如图。
幸好,我学过栈
然后我写了个小程序
第一个方法比较笨,当我写完自己的代码后,看到有同学发了自己的代码,我赶紧就发了个红包,一个是为了鼓励大家多讨论问题,一个是为了赞扬这样的行为。
第一个方法就是取出每一个bit,压栈,然后再出栈,你想怎么排序都没有问题。
#include "stdio.h"
#include "stdlib.h"
struct List{
int data;
struct List * next;
};
struct Stack{
struct List *head;
int size;
};
struct Stack * StackInit(void)
{
struct Stack *stack = NULL;
stack = (struct Stack*)malloc(sizeof(struct Stack));
stack->head = (struct List *)malloc(sizeof(struct List));
stack->head->next = NULL;
stack->size = 0;
return stack;
}
int StackPush(struct Stack *stack,int data)
{
struct List *tmp = (struct List *)malloc(sizeof(struct List));
tmp->data = data;
tmp->next = stack->head->next;
stack->head->next = tmp;
stack->size++;
//printf("push:%d \n",data);
return 0;
}
int IsStackEmpty(struct Stack *stack)
{
if(stack->head->next == NULL){
//printf("stack null\n");
return 1;
}else{
//printf("stack is not null\n");
return 0;
}
}
int StackPop(struct Stack *stack,int *data)
{
struct List *tmp = NULL;
if(IsStackEmpty(stack))
return -1;
tmp = stack->head->next;
*data = tmp->data;
stack->head->next = tmp->next;
stack->size--;
if(tmp!=NULL)
free(tmp);
//printf("pop:%d \n",*data);
return 0;
}
int main(void)
{
int i = 0;
unsigned char a = 0x71;
unsigned char b = 0;
struct Stack *stack = NULL;
stack = StackInit();
printf("a:0x%x\n",a);
for(i = 0;i<8;i++)
{
if(a&0x10) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x20) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x40) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x80) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x01) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x02) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x04) StackPush(stack,1);
else StackPush(stack,0);
if(a&0x08) StackPush(stack,1);
else StackPush(stack,0);
}
for(i = 0;i<8;i++)
{
int d = 0;
StackPop(stack,&d);
if(i == 0)
if(d == 1) b |= 0x80;
if(i == 1)
if(d == 1) b |= 0x40;
if(i == 2)
if(d == 1) b |= 0x20;
if(i == 3)
if(d == 1) b |= 0x10;
if(i == 4)
if(d == 1) b |= 0x08;
if(i == 5)
if(d == 1) b |= 0x04;
if(i == 6)
if(d == 1) b |= 0x02;
if(i == 7)
if(d == 1) b |= 0x01;
}
printf("\nb:0x%x\n",b);
return 0;
}
执行如下:
除了用栈来实现,群里有位同学的答案让我眼前一亮
#include <stdio.h>
struct charbits{
unsigned char data1 : 4;
unsigned char data2 : 4;
};
union dataChange{
struct charbits charBits;
unsigned char data;
}datchange,*p;
int main(){
unsigned char temp;
p = &datchange;
datchange.data = 0x34;
temp = p->charBits.data1;
p->charBits.data1 = p->charBits.data2;
p->charBits.data2 = temp;
printf("data is %x", datchange.data);
return 0;
}
完美演示了位域、共用体、结构体、指针的几个知识点~
测试下位域的作用
#include <stdio.h>
struct charbits{
unsigned char data1 : 1;
unsigned char data2 : 3;
};
struct charbits1{
unsigned char data1;
unsigned char data2;
};
int main(){
struct charbits a;
struct charbits1 b;
printf("%d %d\n",sizeof(a),sizeof(b));
/*赋值1看看*/
a.data1 = 1;
printf("%d\n",a.data1);
/*赋值3看看*/
a.data1 = 3;
printf("%d\n",a.data1);
/*赋值3看看*/
a.data2 = 3;
printf("%d\n",a.data2);
/*赋值7看看*/
a.data2 = 7;
printf("%d\n",a.data2);
/*赋值8看看*/
a.data2 = 8;
printf("%d\n",a.data2);
return 0;
}
data1只能存 0和1,如果存其他值的话就会溢出变成0。
data2是 3bits ,只能存 0~7,我们存 8 进去,就溢出变成 0。
PS:想加入技术群的同学,加了我好友后,就给我发「篮球的大肚子」这句话,有可能机器人打瞌睡,可以多发几次,不要发与技术无关的消息或者推广。
免责声明:本文内容由21ic获得授权后发布,版权归原作者所有,本平台仅提供信息存储服务。文章仅代表作者个人观点,不代表本平台立场,如有问题,请联系我们,谢谢!