clearing bits using C macros

This is a discussion on clearing bits using C macros within the C Programming forums, part of the General Programming Boards category; I am trying to clear bits using a C macro that gets m (clear bit start) and n (# bits ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    28

    clearing bits using C macros

    I am trying to clear bits using a C macro that gets m (clear bit start) and n (# bits to clear)
    This macro needs to receive a reference to a 32-bit register and m and n.

    For example:
    I have a 32 bits value of: 0xFFFFFFFF
    m = 5, n=2
    so I would like my result to be: 0xFFFFFFAF

    but I would like this function-macro to work with any m/n combimation

    #define CLEAR_MN_BITS(reg_x, m, n) (??????????????)

    Can anyone fill in the question marks?

    Thanks!

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Do you know any math or bitwise operators?

    It could be something as simple as ((reg_x) - ((n) * (m) * 16))

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    28
    Quote Originally Posted by whiteflags View Post
    Do you know any math or bitwise operators?

    It could be something as simple as ((reg_x) - ((n) * (m) * 16))
    Yes - I wrote/write a lot of C programs and I did a lot of simple bit manipulations... just never had to write function-macros and hence the problem I encountered by not being able to declare a new variable in my "function"

    can you elaborate more on your answer?
    Last edited by v333k; 04-21-2010 at 06:18 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could always just go read the bitwise FAQ.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Come up with an algorithm for solving this instead of worrying about the coding.
    In your post do you mean bit 5 or the 5th bit because there is a subtle difference.

  6. #6
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Search the forum here for this; I answered this last week with a link to some C source that gives you easy to use macros for doing this...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  7. #7
    Registered User
    Join Date
    Apr 2010
    Posts
    28
    Hi everyone....
    I finally figured it out....

    The proper wording of this problem is:
    clear m bits starting at n-th location

    #define CLEAR_BITS(reg_x, n, m) (reg_x | ~(2^m - 1) << n)

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by v333k View Post
    clear m bits starting at n-th location

    #define CLEAR_BITS(reg_x, n, m) (reg_x | ~(2^m - 1) << n)
    Clearing bits needs the & operator, OR'ing would be used to set the bits.
    So not sure if the CLEAR_BITS function macro does what it is intended for.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    And 2^m is probably not what is intended either.

  10. #10
    Registered User
    Join Date
    Apr 2010
    Posts
    28
    Quote Originally Posted by tabstop View Post
    And 2^m is probably not what is intended either.
    Oh yes - this is the not the XOR operator, it is the 2 to the power of m

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by v333k View Post
    Oh yes - this is the not the XOR operator, it is the 2 to the power of m
    You have that backwards. ^ is XOR. You may mean that you want power of 2, but that's not what you are doing.


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Apr 2010
    Posts
    28
    Quote Originally Posted by quzah View Post
    You have that backwards. ^ is XOR. You may mean that you want power of 2, but that's not what you are doing.


    Quzah.
    Correct - I meant the 2 to the power of m and not XOR

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with bits and SHA-1
    By Desolation in forum C++ Programming
    Replies: 6
    Last Post: 12-28-2008, 01:34 AM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 04:03 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 03:21 PM
  4. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 04:53 PM
  5. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21