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

Recommended Answers

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

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.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.