Thread: Bit Wise Functions

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    17

    Bit Wise Functions

    So I know generally how to use bit-wise functions, but I cannot figure out how to do this...

    I want to change \/
    10100000000000000110001100100000 (num1)
    to \/
    10100000000000000110001100001000 (num2)

    The whole thing remains unchanged except for that one piece. I do not know how to do that using the & and | functions. This isn't so much of a programming problem I guess as to a logic problem. I am simply replacing the value at one address with a 0, and putting a value in another address. And I have no idea how to do it.

    This is not homework, I need it at work and have not done bitwise for a very long time.

    I was thinking something like this...
    Shift 32 1's onto a num3 (num3 = 0xffffffff)
    Shift on 00 (size of each data point in the number)
    Shift over x number of 1's, so the 00's line up with the data to be wiped out
    Do num1 & num3 to wipe out that spot (num1=num1&num3)

    Do the same thing only mask with 0's and put in the desired value instead of 00
    Move over until the wanted value lines up
    do an num1 | num3 and now num3 is in. (num1=num1|num3)


    The problem is I do not know how to do the shifting to make this all work. Or how push 1's and 0's on. Can someone point me in the right direction? Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shift right first BITS - LEFTSHIFT times. Put that into a variable. OR that variable with the original left shifted LEFTSHIFT times.


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

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    14
    What you are trying to do is fairly straight-forward, if I understand the problem properly. To change a 1 into a 0 at position n, we simply take num1^(1<<n) (note: using XOR like this will change a 1 into 0, but it will also change a 0 into 1, so it doesn't guarantee that there will be a 0 at that point after you do the XOR, only that it will be different than it was before). To change a 0 into a 1 at position n we simply take num1|(1<<n) (this always will leave a 1 at position n, even if it is already a 1, since we use OR and not XOR). Here is an illustration of your specific example:
    Code:
    num1		10100000000000000110001100100000
    		00000000000000000000000000001000 	|
    		-------------------------------------
    num1		10100000000000000110001100101000
    
    
    num1		10100000000000000110001100101000
    		00000000000000000000000000100000	^
    		-------------------------------------
    num1		10100000000000000110001100001000
    We could have used XOR both times and gotten to the same result.
    Last edited by Muster Mark; 06-14-2011 at 06:11 PM. Reason: meant |, not &. as pointed out by anduril462, also clarified use of XOR

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Sorry Muster Mark, you're a bit off with your examples. The & one is way off (looks like an |), and your description of ^ is a bit off. You and Prediluted should check out our tutorial: Cprogramming.com - Tutorials - Bitwise Operators and Bit Manipulations in C and C++. And Google for some more examples.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'm not sure understand what you are wanting to do.
    Are you wanting to move bit 5 to where bit 3 is, setting bit 5 to zero in the result? (count bits starting from: 0 is the rightmost one)

    1. If so, first you want to get the original value with the desired bits zeroed out.
    2. Next you want get the desired bit isolated and shift it along.
    3. Lastly you want to combine that bit with the value from step 1.

    Some code for this:
    Code:
    unsigned int a = input & ~0x28;       // 0x28 => 00101000 where those 1's are the bits we want cleared
    unsigned int b = (input & 0x20) >> 2; // 0x20 => 00100000 where the 1 is the bit we want to isloate
    output = a | b;
    Or as a one-liner:
    Code:
    output = (input & ~0x28) | ((input & 0x20) >> 2);
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What do i need network wise?
    By Logikal in forum Tech Board
    Replies: 1
    Last Post: 03-17-2010, 03:00 AM
  2. Bit wise operators
    By scrapper in forum C Programming
    Replies: 1
    Last Post: 02-14-2005, 09:01 AM
  3. graphics please help i'll cry other wise
    By super_monkey in forum Game Programming
    Replies: 4
    Last Post: 11-14-2001, 01:45 AM
  4. Self-adoration (avatar-wise)
    By Govtcheez in forum A Brief History of Cprogramming.com
    Replies: 20
    Last Post: 09-20-2001, 02:34 PM