查看原文
其他

用指针实现高低位倒序,疯了吧?

嵌入式ARM 2021-01-31

The following article is from 嵌入式Linux Author 写代码的篮球球痴

昨晚在微信群看到一个读者发的面试题目,从网上截图出来的,我百思不得其解,题目如图。



幸好,我学过


然后我写了个小程序


第一个方法比较笨,当我写完自己的代码后,看到有同学发了自己的代码,我赶紧就发了个红包,一个是为了鼓励大家多讨论问题,一个是为了赞扬这样的行为。


第一个方法就是取出每一个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。




本文授权转载自公众号“嵌入式Linux”,作者写篮球的代码球痴


-END-




推荐阅读



【01】再谈指针:大佬给你拨开 C指针 的云雾【02】嵌入式编程中的复杂指针的使用【03】C语言指针用得好犹如神助!这些使用技巧值得收藏【04】值得收藏的 C语言 指针讲解文章【05】指针,很难吗?| 工程师给你详细解释!


    您可能也对以下帖子感兴趣

    文章有问题?点此查看未经处理的缓存