I am taking in two params, dwWidth and dwX%8. dwWidth is how many bits wide my number is and dwX%8 is where in the byte it starts. So, if I have dwWidth=3 and dwX%8=0, then I have 111X XXXX. ((Note: It may not be 1's, I'm just using it to demonstrate, it could be equal to 5 and 10101, really all we're interested in is the masking.)) I want to mask what is in these X spots. So, I need to convert this 3 and 0 somehow into a 0001 1111. However, it needs to be general so if dwWidth=4 and dwX%8= 3 I'll have XXX1 111X so I would need to go from a 4 and a 3 to get 1110 0001. This is really difficult and I can't figure it out if it's even possible. Remember, this needs to be arithmetic, I could easily do it using a switch.
Asked
Active
Viewed 64 times
0
-
2I am finding your explanation of the problem exceedingly unclear. A diagram might help. – Alnitak Jul 14 '14 at 19:43
-
I see a neat solution here (depending on the programming language) by taking advantage of the computers built in UTF-8 matrix and unicode index (array). :D – Albert Renshaw Jul 15 '14 at 03:54
-
Also, you probably shouldn't use `%` in your variable name... in most programming languages that represents the modulo function... (which is especially confusing when you are referring to base operations hahahah!) – Albert Renshaw Jul 15 '14 at 03:55
1 Answers
0
If this long winded question means what I think it means, you want a mask of dwWidth
contiguous bits starting at position dwX%8
, where 0 is the most significant bit (who puts %
in variable names...)
def makeMask(width, start):
out = (1 << width) - 1 # field of 1's 'width' wide
out = out << (8 - start - width) # shift into the correct place
return out
>>> x = makeMask(3, 0)
>>> format(x, '#010b')
'0b00011110'
There, all arithmetic.

Ben
- 2,065
- 1
- 13
- 19