PDA

View Full Version : NCD vs. DCD - Commentary



andrewroz
- 3rd November 2007, 00:16
Can someone tell me why it makes sense that:

NCD %00000010 equals 2
BUT
DCD 2 equals %00000100

Why aren't they symetrical?

-Andrew

sinoteq
- 3rd November 2007, 08:13
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

andrewroz
- 6th November 2007, 00:16
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