Is this the proper way to zero the first 12 bits of an address?

void *ans = (void*)((unsigned long)addr &  ((0UL - 1)  ^ 0xfff));

Where addr is any user process memory address..

Note this method works...I just want to know if there is a better way

Edited 7 Years Ago by gerard4143: n/a

I'd just do a bitwise shift left 12 then a bitwise shift right 12

Don't you mean the other way around? Maybe I have a different meaning for first 12

Edited 7 Years Ago by gerard4143: n/a

Here's what I mean
1010101010101111
First twelve is 101010101010
Shift left twelve gives
1111000000000000
Shift right twelve then gives
0000000000001111

Here's what I mean
1010101010101111
First twelve is 101010101010
Shift left twelve gives
1111000000000000
Shift right twelve then gives
0000000000001111

O.K. what I meant was the opposite to what you have here...either way your method works..Thanks

Is this the proper way to zero the first 12 bits of an address?

void *ans = (void*)((unsigned long)addr &  ((0UL - 1)  ^ 0xfff));

Where addr is any user process memory address..

Note this method works...I just want to know if there is a better way

That kind of masking works just fine; it might be expressed more simply:

void *ans = (void*)((unsigned long)addr &  ~0xfffUL);

(At least as far as manipulating bits of an integer. I don't know what address manipulation will get ya.)

This question has already been answered. Start a new discussion instead.