复制位操作的指针
我有一个函数传递一个结构,而不是对 arr 本身进行位操作,我想创建副本。如何复制无符号整数数组的元素以进行位操作?
unsigned int * arr = cs->arr; // cs->arr is set as unsigned int * arr;
unsigned int copy;
memcpy(copy,arr[0], sizeof(unsigned int)); // Copy into copy the first element, for now
int i = 0;
while(copy != 0)
{
i += copy & 1;
copy >>= 1;
}
return i;
谢谢你!
I have a function that passes in a struct that and instead of doing bit manipulations on the arr itself I want to create copy. How can I make a copy of an element of an array of unsigned ints to do bit manipulations on?
unsigned int * arr = cs->arr; // cs->arr is set as unsigned int * arr;
unsigned int copy;
memcpy(copy,arr[0], sizeof(unsigned int)); // Copy into copy the first element, for now
int i = 0;
while(copy != 0)
{
i += copy & 1;
copy >>= 1;
}
return i;
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要
memcopy
。一个简单的数组访问就足够了:You dont need
memcopy
. A simple array access is enough:这就是所需要的一切。
copy
将具有与arr[0]
相同的值,但不会以任何其他方式链接到它。 (即修改copy
不会更改arr[0]
。)is all that's needed.
copy
will have the same value asarr[0]
, but it won't be linked to it in any other way. (i.e. modifyingcopy
will not changearr[0]
.)