Bitwise Operator Help: Trying to build function that returns bits from a binary number using a mask
Mahonroy
Posts: 175
in Propeller 1
Hey guys,
Its been a while since I have messed around with binary operators, and I am having a difficult time wrapping my head around how to accomplish this. An explanation would be awesome as well.
So say I have a number like this: 1110_1010
I want to build a mask and have it return those bits, for example: 0000_1110
Since I have specified a 1 on bit position 1,2 and 3, then this would return those bit positions from the original number, in this case the answer would be: 101
So the function would be in this format: GetBits(binaryNumber,mask). You provide the binary number and mask, and it returns to you the answer.
I am assuming I need to use the bitwise shift operators, but I am just not sure.
I know if I do an & operation, e.g. "1110_1010 & 0000_1110" this would return 1010, so it seems I need to utilize the shift and & operator somehow.
Any help is greatly appreciated, thanks!
Its been a while since I have messed around with binary operators, and I am having a difficult time wrapping my head around how to accomplish this. An explanation would be awesome as well.
So say I have a number like this: 1110_1010
I want to build a mask and have it return those bits, for example: 0000_1110
Since I have specified a 1 on bit position 1,2 and 3, then this would return those bit positions from the original number, in this case the answer would be: 101
So the function would be in this format: GetBits(binaryNumber,mask). You provide the binary number and mask, and it returns to you the answer.
I am assuming I need to use the bitwise shift operators, but I am just not sure.
I know if I do an & operation, e.g. "1110_1010 & 0000_1110" this would return 1010, so it seems I need to utilize the shift and & operator somehow.
Any help is greatly appreciated, thanks!
Comments
If x is a byte variable, you can skip the &.
-Phil
(PASM)
(SPIN)
There may be more clever ways of doing this, but these should clearly show your intent.
From your question, it appears you want the binaryNumber & mask, with the result shifted to the LSB.
Do you want the result shifted based on the first high bit in the mask, the first high bit in the binary number, or the first high bit in the binary result?
Heh. I noticed @Sapphire's answer differed from mine in exactly that way.
Yes, the answer to the question makes a difference.
PS You're too fast. Got it before I edited it.
-Phil
The result should be shifted based on the first high bit in the mask only.
Seairth and Saphire, I think I am understanding this, thanks for the function. So this is basically trimming the end off both the number and the mask until a 1 is reached on the mask, then it returns this as the result? I think the problem is this will return bits after the mask as well.
EDIT: I just re-read the function and noticed that you AND the binary number again with the mask as you are returning it... it looks like this would work. It would not work of the mask had any 0's in the middle... but thats probably fine.
So this is my interpretation of this, let me know if this is correct. You AND the binary number and mask together, which will return all of the bits of the binary number up to the MSB (most significant bit) of the mask. In this case using the example numbers I mentioned above, this would return "1010". It looks like the next goal is to shift the number over X amount of times until the LSB is reached in the mask is that right? So in this case, you are taking the first 32 bits and returning them in reverse order. It then looks like you are taking the number 32 and rotating it to the right by the reversed bits number..... I'm not following this part, can you explain please?
Phil used 32 (bits in a long) to flip your mask with ><. In your case %1110 becomes %01110000_00000000_00000000_00000000. Run this through >| and you get 31 (bit 30 is highest set). Subtract that from 32 and you get the number of bits to right shift for your original mask.
As ever, Phil is a clever dude.