View previous topic :: View next topic |
Author |
Message |
nailuy
Joined: 21 Sep 2010 Posts: 159
|
how to construct a variable by decimal number? |
Posted: Tue Jul 14, 2020 3:59 pm |
|
|
Hy I want to construct variable like: Code: | static unsigned int16 VAR; |
This variable I want to be construct by other variable int8 VAR1, VAR2, VAR3, etc.
a method to construct it may be like this: Code: | VAR=VAR1+(VAR2*10)+(VAR3*100) |
it is a possibility to construct more simple this variable?
like bit_test but in opposite way not in bit mode?
Thank you. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Tue Jul 14, 2020 9:12 pm |
|
|
There are the functions like make16 and so forth. But they do things in groups of 8 bits as opposed to base ten like you have in your example.
So for var=make16(var1,var2) it would be like:
var = var1*256 + var2
Where:
var is int16
var1 and var2 are int8 |
|
|
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Tue Jul 14, 2020 10:23 pm |
|
|
Dear Mr Dluu13 thank you for your reply.
I know function MAKE16.
I need function for decimal construction.
Like:
a=3
b=7
c=6
VAR=Make(abc)
result is 376. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Jul 15, 2020 1:33 am |
|
|
OK.
well you already show the standard way to do this.
There isn't any mathematical shortcut to this. *100, involves calculating
*64+*32+*4 on the source number and adding these. *10, *8 plus *2.
The compiler already optimises the multiplications pretty much as far
as they can go. The only thing you need to do differently, is:
val=a*100l + b*10 + c;
Note the 'l'. This is needed to ensure the digit is converted to an int16
before performing this multiplication, otherwise if the digits are int8 types
there can be an overflow on this multiplication.
Decimals are things that binary processors can't actually do terribly
efficiently. This is why learning to think in binary yourself can improve
coding efficiency so much. Using things like *8 counters instead of *10
etc.. |
|
|
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Wed Jul 15, 2020 2:47 am |
|
|
Dear Telmah.
Thank you for your reply.
For me is okay to know that isn't function for this.
I'll do my function.
This a*100l
l is low "L"
or
l is operator "or" |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Jul 15, 2020 3:31 am |
|
|
l or L.
Case doesn't matter. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9238 Location: Greensville,Ontario
|
|
Posted: Wed Jul 15, 2020 5:08 am |
|
|
l is for long...
It gets to be more 'fun' the older you get..
then the eyes see !,l,|,[,] and I as all being the SAME...
: and ; look the same too..
ah the joys of growing old |
|
|
nailuy
Joined: 21 Sep 2010 Posts: 159
|
|
Posted: Wed Jul 15, 2020 12:21 pm |
|
|
yes temtronic, you are right.
I try to approach to the monitor to see the difference of what did you write.
Thank you for all replies. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Wed Jul 15, 2020 12:31 pm |
|
|
There is also the ting sometimes of choosing a different font. I use a serifed
one, so l has little ticks at the corners making it quite distinct from |. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Wed Jul 15, 2020 12:32 pm |
|
|
Ttelmah wrote: | There is also the ting sometimes of choosing a different font. I use a serifed
one, so l has little ticks at the corners making it quite distinct from |. |
I use Consolas :D
Easy on the eyes |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19535
|
|
Posted: Thu Jul 16, 2020 1:47 am |
|
|
Yes, Consolas, is very much designed to be 'clear', and does distinguish 1, l
& |.
I managed to drop an h in my post. Obviously showing my cockney roots...
|
|
|
|