设置并重置地址的最低有效位 [英] set and reset the least significant bit of a address

查看:71
本文介绍了设置并重置地址的最低有效位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置并重置一个地址的最低有效位(

指针指向的位置)。

我试过这个,但是不正确:

#define BIT 0x1


void foo(){

void * p;

* p = * p& ~BIT

}

I want to set and reset the least significant bit of a address (where a
pointers points to).
I tried this, but it is not correct:
#define BIT 0x1

void foo(){
void *p;
*p = *p & ~BIT
}

推荐答案

onsbomma< on ****** @ hotmail.com>潦草地写道:
onsbomma <on******@hotmail.com> scribbled the following:
我想设置并重置一个地址的最低位(指针指向的位置)。
我试过这个,但是不正确:
#define BIT 0x1
void foo(){
void * p;
* p = * p& ~BIT
}
I want to set and reset the least significant bit of a address (where a
pointers points to).
I tried this, but it is not correct: #define BIT 0x1 void foo(){
void *p;
*p = *p & ~BIT
}




其实,& ~BIT确实是重置LSB的正确方法。 | BIT是

设置它的正确方法。你做错了是用p作为void

指针。 void指针只是一个指针 - 它没有任何

信息,它指向的是什么。您必须使用指向

整数类型的指针。哦,并确保它实际上指向一个

可修改的地址。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰-------- \

\- -------------------------------------------------- -----规则! -------- /

"根据奥卡姆的牙刷,我们只需要优化最常用的

指令。

- Teemu Kerola



Actually, & ~BIT is indeed the right way to reset the LSB. | BIT is the
right way to set it. What you''re doing wrong is using p as a void
pointer. A void pointer is only a pointer - it doesn''t have any
information of what it''s pointing to. You have to use a pointer to an
integer type instead. Oh, and make sure it''s actually pointing at a
modifiable address.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"And according to Occam''s Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola


onsbomma写道:
onsbomma wrote:
我想设置并重置一个地址的最低有效位(其中一个指针指向)。
我试过这个,但是不正确:

#define BIT 0x1

void foo(){
void * p;
* p = * p& ~BIT
}
I want to set and reset the least significant bit of a address (where a
pointers points to).
I tried this, but it is not correct:
#define BIT 0x1

void foo(){
void *p;
*p = *p & ~BIT
}




1)

如果你想改变指针的值而不是值

指向的对象请勿使用*。


2)将指针值转换为无符号整数

存储指针。有一个预定义的类型:intptr_t。


3)

#include< stdint.h>

void * p;

intptr_t ip =(intptr_t)p; //将指针值存储在ip中

ip = ip& 〜1; //消除最不重要的位

p =(void *)ip; //再次将结果存储到指针中。


4)不要写:


p =(void *)((intptr_t)p )& ~1;


因为在2周内你自己会徘徊那些混乱意味着什么。写清楚。



1)
If you want to change the value of the pointer and not the value
of the object being pointed to DO NOT use the "*".

2) convert the pointer value into an unsigned integer capable
of storing a pointer. There is a predefined type for that: intptr_t.

3)
#include <stdint.h>
void *p;
intptr_t ip = (intptr_t)p; // Store the pointer value in ip
ip = ip & ~1; // eliminate the least significant bit
p = (void *)ip; // store the result into the pointer again.

4) Do not write:

p = (void *)((intptr_t)p)&~1;

because in 2 weeks you yourself will be wandering what
that mess means. Write clearly.


> onsbomma写道:
> onsbomma wrote:
。>我想设置并重置地址的最低位
。> (指针指向)。
。>我试过这个,但这不正确:
。>
。> #define BIT 0x1
。>
。> void foo(){
。> void * p;
。> * p = * p& ~BIT


忽略p未初始化,取消引用void指针

是违反约束的。如果你想要修改一个字节,请使用指向unsigned char的指针。


jacob navia写道:1)
如果你想改变指针的值而不是指向的对象的值
不要使用*。

2)将指针值转换为无符号整数
存储指针。有一个预定义的类型:
intptr_t。


但是它只能在C99下使用,并且它是可选的

是否实现定义它。

3)
#include< stdint.h>
void * p;
intptr_t ip =(intptr_t)p; //将指针值存储在ip中
ip = ip& 〜1; //消除最不重要的位
p =(void *)ip; //再次将结果存储到指针中。


与intptr_t之间的转换是实现定义的。

这种方法可能无法做到某些(真实的)

机器。

4)不要写:

p =(void *)((intptr_t)p)& ~1;


的确如此。 ITYM:p =(void *)(((intptr_t)p)&〜(intptr_t)1);

因为在2周内你自己会徘徊那些混乱意味着什么。
.> I want to set and reset the least significant bit of a address
.> (where a pointers points to).
.> I tried this, but it is not correct:
.>
.> #define BIT 0x1
.>
.> void foo(){
.> void *p;
.> *p = *p & ~BIT
Ignoring that p is not initialised, dereferencing a void pointer
is a constraint violation. Use a pointer to unsigned char if you
want to modify a byte.

jacob navia wrote: 1)
If you want to change the value of the pointer and not the value
of the object being pointed to DO NOT use the "*".

2) convert the pointer value into an unsigned integer capable
of storing a pointer. There is a predefined type for that:
intptr_t.
However it is only available under C99 and it is optional as to
whether an implementation defines it.
3)
#include <stdint.h>
void *p;
intptr_t ip = (intptr_t)p; // Store the pointer value in ip
ip = ip & ~1; // eliminate the least significant bit
p = (void *)ip; // store the result into the pointer again.
The conversion to and from intptr_t is implementation defined.
This method may well fail to do what''s expected on some (real)
machines.
4) Do not write:

p = (void *)((intptr_t)p)&~1;
Indeed. ITYM: p = (void *) ( ((intptr_t) p) & ~ (intptr_t) 1 );
because in 2 weeks you yourself will be wandering what
that mess means.




无论如何,我会很困惑地看到第3步。 ;)


跟踪p是否在奇数或

偶数字节并且在适当的时候使用 - 似乎更简单。


-

彼得



I''d be puzzled seeing step 3 anyway. ;)

It would seem simpler to keep track of whether p is at an odd or
even byte and simply use -- where appropriate.

--
Peter


这篇关于设置并重置地址的最低有效位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆