NCD Command

manie

Senior Member
Basic Commands Manual: Section 2 Page 24:Version 6.8

The NCD command is given as:
NCD
The encoder function takes a value and finds the position of the highest bit in
that number that is a 1. Therefore the result will be a number 1 to 16, when bit15
is 1 the answer is 1, when only bit0 is 1 the value is 16. If the value is 0, the result
will be 0.
e.g let b1 = ncd %00001000 (answer b1 = 3)
Questions:

1. Are the bits counted from the left-hand side here ? As I understood the msb is at the left, so why is bit15 given the value 1 ?

2. How would one derive the LOGbase2 of a number in the Picaxe ?

Thanks to the maths gurus for input...
 

eclectic

Moderator
Manie.

For your first question.

There are 17 possible answers.

%0000000000000000 = 0

%0000000000000001 = 1, then to

%1000000000000000 = 16

e

Latest (6.9) Manual 2, p.24

NCD
The encoder function takes a value and finds the position of the highest bit in
that number that is a 1. Therefore the result will be a number 1 to 16, when bit15
is 1 the answer is 16, when only bit0 is 1 the value is 1. If the value is 0, the result
will be 0.
e.g let b1 = ncd %00000100 (answer b1 = 3)
 

Attachments

Last edited:

manie

Senior Member
EC: Thanks. Version 6.8 is in error then.... will get new versions although it eats up bandwidth.

Anybody on question 2 above regarding LOGbase2(number) ?
 

MartinM57

Moderator
Anybody on question 2 above regarding LOGbase2(number) ?
Not a great deal, but:
- what input number range?
- what accuracy? If you just want the integer component, it's the highest set bit number - see the NCD command :) If you want the "real" answer (pun intended) then your into quite a bit of complexity and number scaling to represent the answer accurately
- why (what equation are you trying to implement/aka is there another way round it)?

And will you then want the ALOGbase2 function at the end of the calc?

A minute's googling found http://logbase2.blogspot.com/2007/12/log-base-2.html which may help a bit...or not ;)
 

westaust55

Moderator
NCD – 1 will give you the log base 2 value only at an integer level.

For greater accuracy, consider how the PICAXE trig functions use internal lookup tables. I think that you only option for log-base2 is to do a similar thing for accuracy.

What range of values will you have for the input?
What level of accuracy do you require?

You may need to have the table with values multiplied by say 100 or 1000, for 2 or 3 decimal places of accuracy, in much the same way that the PICAXE trig function look up tables have numbers multiplied by 100.
Have your table in EEPROM with a pointer to the value required and read in as WORD values.
Thus
1 => 0000
2 => 1000
3 => 1585
4 => 2000
5 => 2312
6 => 2585
7 => 2807
8 => 3000
 

manie

Senior Member
Thanks a lot guys: The NCD question was related to the LOG question and provided the answer as I only require it with single bits eg. 2,4,8,16 etc.... so problem solved, should have tried it before asking really but thank you as always.....
 
Top