NCD vs. DCD - Commentary


Closed Thread
Results 1 to 3 of 3

Hybrid View

  1. #1
    Join Date
    Oct 2007
    Posts
    7

    Default NCD vs. DCD - Commentary

    Can someone tell me why it makes sense that:

    NCD %00000010 equals 2
    BUT
    DCD 2 equals %00000100

    Why aren't they symetrical?

    -Andrew

  2. #2
    sinoteq's Avatar
    sinoteq Guest


    Did you find this post helpful? Yes | No

    Default Look in the manual

    NDC has a range of 1-16 but gives a result of 0 if no bit is set. So you can say it has 17 possible results (0-16)
    NDC %00000000 equals 0
    NDC %00000001 equals 1
    NDC %00000010 equals 2
    NDC %00000100 equals 3
    to
    NDC %1000000000000000 equals 16

    NDC %1111111111111111 also equals 16 because NDC is only checking the highest bit set.

    DCD is a bit different it has (0-15) as range

    DCD 0 would give %00000001
    DCD 1 would give %00000010
    DCD 2 would give %00000100

    So you see in DCD you use a BIT reference number and PBP mostly counts bits from 0-7 if it is a byte and 0-15 if it is a word.

    So the easy answer to your question is that NDC and DCD are not symetrical because they are not the reverse function of each other.

    BUT....
    IF you want you can do like this and get a better result.
    Just remember you will "loose" the highest bit but if you use this to set a port it is no problem as long as you do the shifting in a Word size variable.

    Temp Var Word
    A var Byte

    Temp= DCD 0 'Temp=%0000000000000001
    Temp=Temp>>1 'Temp=%0000000000000000
    A=NDC Temp 'A equals 0

    Temp=DCD 15 'Temp=%1000000000000000
    Temp=Temp>>1 ''Temp=%0100000000000000
    A=NDC Temp 'A equals 15
    Last edited by sinoteq; - 3rd November 2007 at 07:45.

  3. #3
    Join Date
    Oct 2007
    Posts
    7


    Did you find this post helpful? Yes | No

    Default NCD vs. DCD

    Thank you, Sinoteq.

    Nicely stated.

    I guess I understood their function, I just wondered why PicBasic (or whoever) hadn't created the functions in a symmetrical manner. But I guess you have a point in that, they may have tried to make them symmetrical but then "NCD 0001" and "NCD 0000" would have had the same answer (0). So they wouldn't have had a way to indicate when NO bits where set.

    More advanced languages allow for -1 as an answer to a function, which might have given them room to make the 2 functions symmetrical.

    oh well.
    Thanks.

    -Andrew

Similar Threads

  1. DCD command - need some advice
    By malc-c in forum mel PIC BASIC Pro
    Replies: 20
    Last Post: - 18th December 2006, 03:43
  2. Music notes and DIG, DCD, and NCD ?
    By websmith in forum General
    Replies: 2
    Last Post: - 9th November 2006, 14:28
  3. Use of DIG, DCD, NCD
    By websmith in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th November 2006, 06:14
  4. Pic Basic Pro Syntax
    By Finn in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 26th January 2004, 18:07

Members who have read this thread : 0

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