0

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 by gerard4143: n/a

3
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by Dave Sinkula
0

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 by gerard4143: n/a

0

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

0

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

0

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.