Public Group

# BitWise Operations

This topic is 4323 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello everyone, i'll start with a little explanation, im a little lost with bitwise operations, i have an int value of 115, i want to obtian a single bit from this number, using the binary shift operator, this works fine. I then want to proceed and change the LSB in another number to match the bit i just extracted, therefore modifying the number by 1. An explanation: First number 0 1 1 1 0 0 1 1 << I extract the first(right most) 1 from here Second number 0 0 1 0 1 1 1 0 << I want to make this right most bit equal to the bit i extracted from the first number therefore i would have third number 0 0 1 0 1 1 1 1 i just cant get my head around everything, so far i have
				BinNumber = (shiftedNumber >> i) & 1; // rightmost bit from first number

BinNumber2 = (ShiftedColour >> 0) & 1;
BinNumber2 = BinNumber;


This works in that i obtain the right most bit from the first number, then mak an int equal to it, however it doesnt change the value stored in the second int, only makes the whole BinNumber2 equal to the right most bit i extracted earlier..... i hope that makes sense, it does to me, but then again the whole process doesn't there must be an easy way to just set the LSB bit in an int to either 0 or 1??

##### Share on other sites
SecondNumber |= (FirstNumber & 1);

That should do it.

##### Share on other sites
Quote:
 Original post by DaveSecondNumber |= (FirstNumber & 1);That should do it.

It appears to just give me the same number o.o

i use
	secondNumber|= ( (FirstNumber >> i) & 1);

where i = 0 since its in a loop, can you see any reason why?

##### Share on other sites
Have you forgotten some arithmetic before the "=" sign in BinNumber2 = BinNumber; --Oh no, it could not be so simple :)?
Cheers
StratBoy61

##### Share on other sites
I'm sorry, that was stupid of me, the data is unchanged because im changing a 1 to a 1, which is obviously a 1, well thanks for your help. Cleared a couple of things up for me..

##### Share on other sites
Quote:
 Original post by DaveSecondNumber |= (FirstNumber & 1);That should do it.

This will only work if you want to change 0->1 but not the other way round. Try this:
second = second & ~1 | first & 1;

This will clear the lsb and then set it to the fsb of the other number.

##### Share on other sites
i seem to have encounter another problem, i will explain by drawing
the sequence of bits i would like to change is like this
1 0 0 0 1 1 1 1
and i need them to be like this 1 1 0 0 1 1 1 0

Or'ing them like said, i get his 1 1 0 0 1 1 1 1
which is not what i would like to do, is ther any other way to do this calculation or is there something i am missing?

##### Share on other sites
Quote:
Original post by DevFred
Quote:
 Original post by DaveSecondNumber |= (FirstNumber & 1);That should do it.

This will only work if you want to change 0->1 but not the other way round. Try this:
second = second & ~1 | first & 1;

This will clear the lsb and then set it to the fsb of the other number.

thanks a lot DevFred, that sorted my problem its great :) I s there somewhere i can post my end product to have it evaluated? i use it for steganography

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 28
• 16
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
634111
• Total Posts
3015575
×