# Bit Wise Functions

This is a discussion on Bit Wise Functions within the C Programming forums, part of the General Programming Boards category; So I know generally how to use bit-wise functions, but I cannot figure out how to do this... I want ...

1. ## 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. Shift right first BITS - LEFTSHIFT times. Put that into a variable. OR that variable with the original left shifted LEFTSHIFT times.

Quzah.

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

4. 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. 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);`