gpt4 book ai didi

c - 如何确保两个地址的最低有效 4 位相同?

转载 作者:行者123 更新时间:2023-11-30 18:22:43 26 4
gpt4 key购买 nike

所以我有两个指针:

unsigned char * a;
unsigned char * b;

假设我使用了 malloc 并且它们被分配了一定的大小。我想让指针地址的最低有效 4 位相同...但我真的不知道怎么做。

首先,我想从 a 中取出最低有效 4 位。我尝试过类似的东西

int least = (&a) & 0x0f;

但我收到一个错误,指出 & 是无效操作数。我正在考虑为 b 分配更多,并搜索具有与 a 相同的最低有效 4 位的地址,但我真的不知道如何做到这一点。

最佳答案

#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>

int main()
{
unsigned char *a;
unsigned char *b;

a = malloc(8);
b = malloc(8);

if (((uintptr_t)a & 0x0F) == ((uintptr_t)b & 0x0F)) {
printf("Yeah, the least 4 bits are the same.\n");
} else {
printf("Nope, the least 4 bits are not the same.\n");
}

free(a);
free(b);

return EXIT_SUCCESS;
}

关于c - 如何确保两个地址的最低有效 4 位相同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16284121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com