gpt4 book ai didi

c - 如何使用C将奇数和偶数位置位存储到另一个指针?

转载 作者:行者123 更新时间:2023-11-30 21:21:39 28 4
gpt4 key购买 nike

我有两个指针。一个是输入 ptr,另一个是输出..

我的输入指针包含二进制值

15.................0
0011 0110 1111 0111 - unsigned 16 bit
1011 0100 1011 1100 - unsigned 16 bit

首先,我想从两行中获取偶数位置位,并且必须将其填充到输出指针中。

o/p 指针将是

  0110  0110  0110  1111   (even position bits from both)..from right to left and assume 0 to 15.
1100 1110 0101 1101 (odd position bits from both)

如何使用指针来做到这一点?我对指针很陌生

最佳答案

将代码转换为使用指针通常非常简单。

举个例子:

 uint16_t compute(uint16_t a, uint16_t b)
{
return ((a | b) & MY_BIT_MASK) >> (a & 2); // some arbitrary operations
}

uint16_t compute_p(uint16_t *a, uint16_t *b)
{
return ((*a | *b) & MY_BIT_MASK) >> (*a & 2); // some arbitrary operations via pointers
}

如果您向我们展示您的实际代码,我们也许可以为您提供更多帮助。但基本上,当你有一个指向 uint16_t 的指针时,你可以通过两种方式使用它:

 uint16_t *p;

p <- to mean the pointers value, which is an address, you assign it to other pointers, or increment it, so it would point to the next uint16_t in memory
*p <- to use it in expressions on place of a uint16_t, this way your code will each time fetch the actual 16 bit value pointed to by p
p[i] <- with int i, This is basically a synonym to *(p+i) as long as i is non-negative
*(p + i) <- with int i, This is also the value of 16 bit integer where p points to, or the next, or previous etc 16 bit value. In this form, negative i can be used as well
p - q <- with uint16* q the difference of the pointers, in case of uint16_t * types, this basically means "how many 16 but ints I can store and addresses q, q+1, q+2, q+3 .... p-1", assuming p>q

关于c - 如何使用C将奇数和偶数位置位存储到另一个指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22578772/

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