PDA

View Full Version : Bit masking rules



muddy0409
- 10th March 2009, 13:50
I have not done any masking of bits for some 30 years, and it seems to me that the masking rules have changed in PBP?
My memory of this is : to mask off the high nibble of a byte, the unwanted nibble would be ANDed with "0"s and the wanted nibble would be ANDed with "1"s IE %01100101 AND %00001111 would = %00000101 that is the high nibble would be ignored while the lower nibble would be effectively unchanged. However, the way I read the manual the operation is oposite.

indata = %01011011
debug "raw data ",bin8 indata," "
outdata = indata and %11110000
indata = indata and %00001111
debug "indata ",bin8 indata," "
debug "outdata ",bin8 outdata

.....Is what I have been playing around with, but the first byte comes out correct (No modification so it should) Both the next two results come out as % 11111111
This seems to be way out of kilter with my memory of ANDing stuff with real logic gates (remember them?)

Once again I figure I'm doing something wrong, but damned if I can figure out what.

I have some stuff feeding the lower 4 bits of porta and i want to mask off the higher bits so I can come up with a straight HEX digit, but it don't appear to be working. Dammit!!

If I don't mask anything and simply debug hex1 indata it is OK, but using the whole byte is not useful as the other bits are also active with other stuff that I don't want at the time.

Another night of frustration coming up, I see.

Thanks in advance for any pointers folks.

Byte_Butcher
- 10th March 2009, 14:44
Well, I'm a serious newbie myself, but I *think* you need to use the Bitwise Operator "&", not the logical operator "AND".

Try:

outdata = indata & %11110000


Edit: yeah, I just checked pgs 39 and 40 of the PBP manual (printed version) and it describes the difference between "&" and "AND". You definitely want "&".


steve

muddy0409
- 10th March 2009, 16:48
Well I did warn you it would be somthing bloody simple.
Spot on.
Thanks heaps.
Now for some sleep!!