944,033 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 498
  • C RSS
Nov 7th, 2009
0

Zeroing bits

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

  1. 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
Last edited by gerard4143; Nov 7th, 2009 at 12:05 pm.
Similar Threads
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Nov 7th, 2009
0
Re: Zeroing bits
I'd just do a bitwise shift left 12 then a bitwise shift right 12
Reputation Points: 16
Solved Threads: 16
Junior Poster in Training
kolosick.m188 is offline Offline
72 posts
since Jun 2009
Nov 7th, 2009
0
Re: Zeroing bits
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
Last edited by gerard4143; Nov 7th, 2009 at 1:54 pm.
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Nov 7th, 2009
0
Re: Zeroing bits
Here's what I mean
1010101010101111
First twelve is 101010101010
Shift left twelve gives
1111000000000000
Shift right twelve then gives
0000000000001111
Reputation Points: 16
Solved Threads: 16
Junior Poster in Training
kolosick.m188 is offline Offline
72 posts
since Jun 2009
Nov 7th, 2009
0
Re: Zeroing bits
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
Reputation Points: 499
Solved Threads: 367
Postaholic
gerard4143 is offline Offline
2,197 posts
since Jan 2008
Nov 7th, 2009
0
Re: Zeroing bits
Click to Expand / Collapse  Quote originally posted by gerard4143 ...
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.)
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Problem with Bitwise Masking
Next Thread in C Forum Timeline: Problem with piping commands in C





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC