0

I want to learn if this is possible:

for ex:

we have long Lvalue = 0xFF00f41a;

and also have int *p;

Can we point to last 2 byte of Lvalue

like p=&Lvalue <<16;

p pointed frist 16 bit value is it possible?

*p --> f41a;
*(p+1) --->0xFF00;

then if

p = 0xa011;

long Lvalue ---> 0xFF00a011

actually I need bit operations. I have 32 bit value but I can send only 16 bits and if I change 16 bit have to change first 16 bit last 16 bit of 32 bit value.

Jens
  • 69,818
  • 15
  • 125
  • 179
dev.burak
  • 13
  • 4
  • 3
    Your question is very confused. What do you want and how is that related to pointers? See [ask] and provide a [mcve] – too honest for this site Apr 27 '16 at 12:42
  • If I understood it correctly, you want to change the lower `int` part of a `long` through pointers; no, this is not possible. It breaks the strict-aliasing rule. – edmz Apr 27 '16 at 12:49

2 Answers2

3

If you just want 16bits of the larger 32bits type, use bit mask for the task, for example, to get the lower 16 bits of value:

long value = 0xFF00f41a;
long lower = value & 0xFFFF;

To change the lower 16 bits of value, use bit operation:

lower = <some thing new>;
value = (value & 0xFFFF0000) | lower;

Don't use pointer to access part of the 32bit value, it crates undefined bahavior when you dereference it.

fluter
  • 13,238
  • 8
  • 62
  • 100
1

Following short example will work, if int is aligned on 4-bytes (which seems to be guaranteed by gcc, see Casting an int pointer to a char ptr and vice versa):

#include <stdio.h>                                                                                                                                                                                             

int main() {                                                                                                                                                                                                   
    int v = 0xcafe;                                                                                                                                                              
    int *ip = &v;                                                                                                                                                                                              
    char *cp = (char*) ip;                                                                                                                                                                                     

    printf("%hhX\n", *cp); // FE on little-endian box                                                                                                                                                                                    
    printf("%hhX\n", *(cp + 1));                                                                                                                                                                               

    *cp = 0xbe;                                                                                                                                                                                                
    *(cp + 1) = 0xba;                                                                                                                                                                                          
    printf("%X\n", *ip);                                                                                                                                                                                       

    return 0;                                                                                                                                                                                                  
}

You can guarantee alignment of int thus:

int main() {                                                                                                                                                                                                   
    __attribute__ ((aligned (4))) int v = 0xcafe;                                              
Community
  • 1
  • 1
Victor Sorokin
  • 11,878
  • 2
  • 35
  • 51