473,785 Members | 2,209 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

set and reset the least significant bit of a address

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
}
Nov 14 '05 #1
52 7073
onsbomma <on******@hotma il.com> 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 (pa*****@cc.hel sinki.fi) ------------- Finland --------\
\-------------------------------------------------------- rules! --------/
"And according to Occam's Toothbrush, we only need to optimise the most frequent
instructions."
- Teemu Kerola
Nov 14 '05 #2
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.

Nov 14 '05 #3
> 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

Nov 14 '05 #4
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.
Nov 14 '05 #5
You are right, the intptr_t is optional. Sorry.
In the other post I stated that they weren't.

jacob
Nov 14 '05 #6
In article <42************ *********@news. wanadoo.fr>,
jacob navia <ja***@jacob.re mcomp.fr> 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
Nov 14 '05 #7
In article <42************ *********@news. wanadoo.fr>,
jacob navia <ja***@jacob.re mcomp.fr> 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
Nov 14 '05 #8
jacob navia <ja***@jacob.re mcomp.fr> 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_Keit h) ks***@mib.org <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.
Nov 14 '05 #9
In article <42************ *********@news. wanadoo.fr>,
jacob navia <ja***@jacob.re mcomp.fr> 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.

Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7756
by: Eric Kincl | last post by:
Hey, I was wondering if it's possible to reset the "auto_increment" feature in MySQL. For example, if I have a database with a list that will change every year (the old list be completely flushed out, deleted) and then I insert a new list, it will very quickly reach the max INDEX length... Does it just wrap itself? Is there a way to reset "auto_increment" so that I can flush all the rows, reset auto_increment, and start from scratch? ...
4
11194
by: Donnal Walter | last post by:
On Windows XP I am able to connect to a remote telnet server from the command prompt using: telnet nnn.nnn.nnn.nnn 23 where nnn.nnn.nnn.nnn is the IP address of the host. But using telnetlib, this code returns the traceback that follows: import telnetlib host = 'nnn.nnn.nnn.nnn'
16
20532
by: John Baker | last post by:
Hi: I know this is a strange question, but I have inherited a system where files are copied and records re auto numbered (as an index field) )frequently, and I am wondering how high the number can go without the system crashing. An ancillary question is how one resets an auto number so that the sequence starts again at 1. In the case of this file, the auto number field serves no useful purpose except as an
8
2178
by: spike | last post by:
My prygram goes through a string to find names between '\'-characters The problem is, parts of the name in sTemp remains if the new name is shorter than the old name. code --------------------------------------------------- char sTemp; char sText = "THELONGESTNAME\\samantha\\gregor\\spike\\..."; // and so on...
20
9176
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
0
1433
by: Larry Serflaten | last post by:
I recently fired up VS2003 while online which I have done on numerous occasions before. When I opened the toolbox, ZoneAlarm popped up a box saying the IDE wanted to access the internet. This was the first time it tried to connect to the internet, that I can recall, and it was to some address I did not recognise, so I denied it. VS hung, and when I tried to kill the process (Task Manager) all my desktop windows went into tiled view...
3
18749
by: kashif_khan | last post by:
hi everyone, can anybody please tell me why reset button is not working in the following code, i tested it in opera, firefox and konqueror, <form action='edit_links.php' method='POST' name='save_links' id='save_links'> <table cellpadding='5'>
4
7977
by: thegeneralguy | last post by:
Hello, I'm trying to find the least significant set bit (i.e. the first '1' bit in an int) through just bit twiddling, but I can't get it without resorting to a loop. My original idea was to reverse the bits in an int and find the most significant bit then subtract it from 31 to get the position of the LSB. Is there a way to get the LSB of an int using just bit manipulations? e.g., 0b0101 would return 0001, 0b0000 would return...
2
3205
by: DarthPeePee | last post by:
Hello everyone. I am working on a Password Strength Meter and I am running into 1 problem that I would like to fix. When pressing the "Clear Password & Try Again" button, the password clears out of the text box, but the meter will stay at its current position until text is entered back into the textbox. Once text is re-entered, the meter will display the results again. I would like everything to reset when the button is pushed, but I...
0
9646
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7505
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5518
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.