Bit masking rules


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216

    Default Bit masking rules

    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.
    Peter Moritz.
    Up the bush, Western Plains,
    New South Wales,
    Australia.

  2. #2
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by Byte_Butcher; - 10th March 2009 at 14:50.

  3. #3
    Join Date
    Jun 2005
    Location
    Up the bush, Western Plains, NSW Au
    Posts
    216


    Did you find this post helpful? Yes | No

    Default

    Well I did warn you it would be somthing bloody simple.
    Spot on.
    Thanks heaps.
    Now for some sleep!!
    Peter Moritz.
    Up the bush, Western Plains,
    New South Wales,
    Australia.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. Doubt with interrupt on change
    By lugo.p in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th March 2010, 15:22
  3. Sleep Mode
    By Pesticida in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th March 2008, 10:31
  4. PICBasic newbie problem
    By ELCouz in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 12th February 2008, 00:55
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts