CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

how to construct a variable by decimal number?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
nailuy



Joined: 21 Sep 2010
Posts: 159

View user's profile Send private message

how to construct a variable by decimal number?
PostPosted: Tue Jul 14, 2020 3:59 pm     Reply with quote

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? Smile
Thank you.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Tue Jul 14, 2020 9:12 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Tue Jul 14, 2020 10:23 pm     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Wed Jul 15, 2020 1:33 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 15, 2020 2:47 am     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Wed Jul 15, 2020 3:31 am     Reply with quote

l or L.

Case doesn't matter.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Jul 15, 2020 5:08 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jul 15, 2020 12:21 pm     Reply with quote

yes temtronic, you are right.

Laughing Rolling Eyes Razz Laughing

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: 19195

View user's profile Send private message

PostPosted: Wed Jul 15, 2020 12:31 pm     Reply with quote

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 |. Very Happy
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Wed Jul 15, 2020 12:32 pm     Reply with quote

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 |. Very Happy


I use Consolas :D
Easy on the eyes
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Jul 16, 2020 1:47 am     Reply with quote

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...
Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group