loudking said:
data is a pointer with data-type char pointing to an address starting
with 0x81F4 blabla
What precisely do you mean by that? How did you determine what the
address is? In particular, what does the code look like that you used to
print out that string of hexedecimal digits, and what does the "blabla"
actually represent? Several things you say down below suggest that what
you're looking at is not what I'd expect, but I haven't been able to
figure out what your are looking at.
what I what is to get 0x1F4 out of data.
Why? The reason that I ask is that most of the likely reasons for
wanting to look at the actual representation of a pointer are bad ones.
In particular, why are you interested in extracting only 12 bits of the
pointer?
I'm assuming that you want to extract 12 bits. Your example is also
consistent with wanting to extract as few as 7 consecutive bits, or 6
non-consecutive bits, or a number of other even less likely operations.
That's one of the problems with describing what you want to do by giving
an example, rather than by actually explaining it.
if data is plused by one, I will get 0xF4 blabla
That is rather odd, depending upon what you mean by "0x81F4 blabla".
if data is casted to short, I will get 0xF481
Converting a pointer to a integer type other than intptr_t or uintptr_t
is seldom meaningful. Even conversion to one of those two types is not
portable, though it often has a non-portable meaning.
if data is "AND" with 0xfff, I will get a segmentation vialation.
Attempting to apply binary & on a pointer is a constraint violation, and
most implementations won't even compiler such code. If an implementation
does choose to compile it, the resulting behavior is undefined, by
reason of the absence of a specific definition for the behavior. So a
segmentation violation is entirely possible, though it seems very unlikely.
I suspect that you're not correctly describing what your code actually
does; a segmentation violation suggests that you tried to store the
result of your AND operation in a pointer, and then tried to use that
pointer. No matter what you did to create that pointer value, if it even
vaguely resembles what you've been telling us, it's not guaranteed to be
a valid pointer value, and is pretty likely to be invalid.
It would be a lot clearer if you give us the actual code that produced
the segmentation violation.
Could anybody tell me what to do please?
In the unlikely circumstance that you actually have a good reason for
doing this, you're probably using an implementation where conversion of
a pointer to uintptr_t will give you a number that, in hexadecimal,
looks like "0x81F4 blabla", as you put it. In that case, you can just
perform bitwise manipulations on the uintptr_t variable to extract 0x1F4.