Pointer and BitWise Operator.

C

Che

Hi,

I am just curious if anybody ever used a pointer to an integer(any
datatype) and then manipulate the same with bitwise operators.

For example

short int *ptr;
short int q;
int final;

ptr=&q;
final=*ptr & 0xff;

This is just one vague example. I am curious to know if their are any
instances in real time programing where you may have to point to a
register/variable and then manipulate the same using bitwise
operators.

Please elaborate with a more realistic example. Will appreciate it.

Thanj you for the time.
Che
 
M

Malcolm

Che said:
I am just curious if anybody ever used a pointer to an integer(any
datatype) and then manipulate the same with bitwise operators.
Pointers are opaque, so any attempt to access their bit patterns is
non-portable.
You might use this to test alignment. For instance, your malloc() might
return pointers always aligned on 8 bytes. To test if a pointer is correctly
aligned you might want to check the lower three bits. However if you try to
port the program to a different machine that doesn't have this harware
restriction the code will break.
 
C

CBFalconer

Che said:
I am just curious if anybody ever used a pointer to an integer
(any datatype) and then manipulate the same with bitwise operators.
.... snip ...

(Found in c.l.c)

You posted this in at least one other group (c.a.e) and were
answered there. DO NOT multi-post. There is no reason I (or
others) should have to read your question multiple times.

If you must query multiple groups at least cross-post, and set
follow-ups to one of those groups alone. I have set the postings
and followups of this reply to what you should have used in the
first place, except you might have chosen c.l.c for the followup.
 
C

CBFalconer

Malcolm said:
Pointers are opaque, so any attempt to access their bit patterns is
non-portable.

His example was manipulating things that the pointer pointed to,
not the pointer proper.
 
B

Barry Schwarz

Pointers are opaque, so any attempt to access their bit patterns is
non-portable.
You might use this to test alignment. For instance, your malloc() might
return pointers always aligned on 8 bytes. To test if a pointer is correctly
aligned you might want to check the lower three bits. However if you try to
port the program to a different machine that doesn't have this harware
restriction the code will break.
He wasn't accessing the bit pattern of the pointer. He was accessing
the bit pattern of the int pointed to. His sample code which you
chose not to quote
short int *ptr;
short int q;
int final;
ptr=&q;
final=*ptr & 0xff;

The only problem is q needs to be initialized with a value before it
used as part of an expression.


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top