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

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

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

## All 5 Replies

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

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

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));``

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