Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > set and reset the least significant bit of a address

Reply
Thread Tools

set and reset the least significant bit of a address

 
 
onsbomma
Guest
Posts: n/a
 
      03-15-2005
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
}
 
Reply With Quote
 
 
 
 
Joona I Palaste
Guest
Posts: n/a
 
      03-15-2005
onsbomma <(E-Mail Removed)> scribbled the following:
> 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
> }


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 ((E-Mail Removed)) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      03-15-2005
onsbomma wrote:
> 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)
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.



 
Reply With Quote
 
Peter Nilsson
Guest
Posts: n/a
 
      03-15-2005
> onsbomma wrote:
>.> 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.


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

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      03-15-2005
Peter Nilsson wrote:
> The conversion to and from intptr_t is implementation defined.
> This method may well fail to do what's expected on some (real)
> machines.
>


The C standard says (page 256)
<<
The following type designates a signed integer type with the
property that any valid pointer to void can be converted to
this type, then converted back to pointer to void,
and the result will compare equal to the original pointer:

intptr_t

<<

1) It is *not* optional.
2) If it fails to do what is specified this is a broken
implementation.

The current C standard is C99. Of course there are people
that want to destroy C and make it go back to 1969
when it didn't exist. I hope you are not one of them.

What I fail to understand are the people that always
speak about "Standard C" but they mean some inexistent
standard. The C89 Standard doesn't exist any more.

It was replaced by the current one.


 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      03-15-2005
You are right, the intptr_t is optional. Sorry.
In the other post I stated that they weren't.

jacob
 
Reply With Quote
 
Walter Roberson
Guest
Posts: n/a
 
      03-15-2005
In article <42376483$0$837$(E-Mail Removed)>,
jacob navia <(E-Mail Removed)> wrote:
:The current C standard is C99. Of course there are people
:that want to destroy C and make it go back to 1969
:when it didn't exist. I hope you are not one of them.

:What I fail to understand are the people that always
:speak about "Standard C" but they mean some inexistent
:standard. The C89 Standard doesn't exist any more.

:It was replaced by the current one.

Tell me, did your house / apartment stop existing when they
last revised the relevant Construction Code [e.g., required
that certain kinds of nails be 1mm thicker] ?
--
Beware of bugs in the above code; I have only proved it correct,
not tried it. -- Donald Knuth
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      03-15-2005
In article <42376483$0$837$(E-Mail Removed)>,
jacob navia <(E-Mail Removed)> wrote:
>What I fail to understand are the people that always
>speak about "Standard C" but they mean some inexistent
>standard. The C89 Standard doesn't exist any more.


Standards are for our benefit, not the standards bodies'. If we
continue to use C89, it still exists.

-- Richard
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-15-2005
jacob navia <(E-Mail Removed)> writes:
[...]
> The current C standard is C99. Of course there are people
> that want to destroy C and make it go back to 1969
> when it didn't exist. I hope you are not one of them.


That's nonsense, as you should know if you've been paying any
attention at all in this newsgroup.

> What I fail to understand are the people that always
> speak about "Standard C" but they mean some inexistent
> standard. The C89 Standard doesn't exist any more.


My copy of it certainly exists.

> It was replaced by the current one.


Yes, the C90 standard was officially superseded by the C99 standard,
but there are still numerous implementations that conform reasonably
well to the C90 standard, and relatively few that conform to the C99
standard. Given that reality, it's perfectly reasonable to continue
discussing C90, however much we might wish (or not) that C99 had
achieved universal coverage.

That does not in any way constitute wanting to destroy C.

You're complaining about someone mentioning that intptr_t is specific
to C99. Would you prefer that we post answers that assume everyone
has a C99 compiler, so they can come back and ask us what's wrong when
it doesn't work with their C90 compilers?

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Christian Bau
Guest
Posts: n/a
 
      03-16-2005
In article <42376483$0$837$(E-Mail Removed)>,
jacob navia <(E-Mail Removed)> wrote:

> Peter Nilsson wrote:
> > The conversion to and from intptr_t is implementation defined.
> > This method may well fail to do what's expected on some (real)
> > machines.
> >

>
> The C standard says (page 256)
> <<
> The following type designates a signed integer type with the
> property that any valid pointer to void can be converted to
> this type, then converted back to pointer to void,
> and the result will compare equal to the original pointer:
>
> intptr_t
>
> <<
>
> 1) It is *not* optional.


To quote the C99 Final Draft:

7.18.1.4 Integer types capable of holding object pointers

1 The following type designates a signed integer type with the property
that any valid pointer to void can be converted to this type, then
converted back to pointer to void, and the result will compare equal to
the original pointer:

intptr_t

The following type designates an unsigned integer type with the property
that any valid pointer to void can be converted to this type, then
converted back to pointer to void, and the result will compare equal to
the original pointer:

uintptr_t

These types are optional.
=========================

> 2) If it fails to do what is specified this is a broken
> implementation.


Apparently not.

> The current C standard is C99. Of course there are people
> that want to destroy C and make it go back to 1969
> when it didn't exist. I hope you are not one of them.
>
> What I fail to understand are the people that always
> speak about "Standard C" but they mean some inexistent
> standard. The C89 Standard doesn't exist any more.
>
> It was replaced by the current one.
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
most significant and less significant address a01lida VHDL 2 11-16-2008 12:28 PM
Need regular expression for at least 7 characters and at least 1 special chatacter AAaron123 ASP .Net 0 10-03-2008 01:25 PM
least significant bit ? q666 Ruby 1 03-01-2007 08:30 AM
Significant Digits (Significant Figures) SMH Javascript 0 01-07-2007 09:52 AM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments