Maker Pro
Maker Pro

Getting Decimal value instead of hex

BHARGAV SHANKHALPARA

Nov 6, 2013
35
Joined
Nov 6, 2013
Messages
35
Hi all...

i am programming ATMEGA32 microcontroller and ATMEL STUDIO 6 for programming in my application.

in my application user enter 3 difference value which i have store in char variable.

At the end i combine this 3 value in one variable and compare it with predefine value. when i combine in one variable it automatically convert into decimal.

for example i have 3 variable named S1,S2,S3

user enter S1=2,S2=0,S3=9;

at end combine value in TOTAL variable which is INT.

TOTAL = (S3 | (S2 << 4) | (S1 << 8));

so TOTAL value should be 209

but it shows after decimal conversation 521.

when i compare it with 209 this gives no result, but it gives result when my predefine value 521.

i want to get result when user enter 209 and my predefine value also 209.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
so TOTAL value should be 209
but it shows after decimal conversation 521.

S1=2, S1 << 8 = S1*256 = 2*256 = 512
S2=0, S2 << 4 = S2* 16 = 0*16 = 0
S3=9

-> (S3 | (S2 << 4) | (S1 << 8)) = 521, not 209.

decimal 521 = 0x209 (hex).


The issue is with the shift statements. You want the decimal number 209 to be entered as single digits in the sequence 2*100+0*10+9*1. Therefore you cannot use shift operations, which will multiply by powers of 2.
You need to construct the INT representation from the input as follows:

TOTAL = 100*S1+10*S2+S3;

If your math library is too slow, you can speed up the operations for *10 and *100 by substituting it by

Code:
10_times(int x)
{
return x<<3 + x<1; /* x<<3 = x*8, x<<1 = x*2 */
}

100_times(int x)
{
return 10_times(x)*10_times(x)
}

You can also use macros (#define ...) to avoid the subroutine call and generate inline code instead.
 

BHARGAV SHANKHALPARA

Nov 6, 2013
35
Joined
Nov 6, 2013
Messages
35
S1=2, S1 << 8 = S1*256 = 2*256 = 512
S2=0, S2 << 4 = S2* 16 = 0*16 = 0
S3=9

-> (S3 | (S2 << 4) | (S1 << 8)) = 521, not 209.

decimal 521 = 0x209 (hex).


The issue is with the shift statements. You want the decimal number 209 to be entered as single digits in the sequence 2*100+0*10+9*1. Therefore you cannot use shift operations, which will multiply by powers of 2.
You need to construct the INT representation from the input as follows:

TOTAL = 100*S1+10*S2+S3;

If your math library is too slow, you can speed up the operations for *10 and *100 by substituting it by

Code:
10_times(int x)
{
return x<<3 + x<1; /* x<<3 = x*8, x<<1 = x*2 */
}

100_times(int x)
{
return 10_times(x)*10_times(x)
}

You can also use macros (#define ...) to avoid the subroutine call and generate inline code instead.

hi
Harald Kapp...

Thank you so much...as you told i have done. its working correctly.

Thanks again
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
I'm sorry, I detected a flaw in my code:
Code:
10_times(int x)
{
return x<<3 + x<1; /* x<<3 = x*8, x<<1 = x*2 */
}


100_times(int x)
{
/* wrong code :
return 10_times(x)*10_times(x); -> this will give 100*x^2
*/
/* correct code */
return 10_times(10_times(x)); /* this will give 10*10*x = 100*x
}
 

BHARGAV SHANKHALPARA

Nov 6, 2013
35
Joined
Nov 6, 2013
Messages
35
I'm sorry, I detected a flaw in my code:
Code:
10_times(int x)
{
return x<<3 + x<1; /* x<<3 = x*8, x<<1 = x*2 */
}


100_times(int x)
{
/* wrong code :
return 10_times(x)*10_times(x); -> this will give 100*x^2
*/
/* correct code */
return 10_times(10_times(x)); /* this will give 10*10*x = 100*x
}

But i have done this way...

TOTAL = 100*S1+10*S2+S3;

and its working good...!!

Thanks...
 

kpatz

Feb 24, 2014
334
Joined
Feb 24, 2014
Messages
334
If your math library is too slow, you can speed up the operations for *10 and *100 by substituting it by...

Fixed a typo in 10_times below:

Code:
10_times(int x)
{
return x<<3 + x<<1; /* x<<3 = x*8, x<<1 = x*2 */
}

100_times(int x)
{
return 10_times(x)*10_times(x)
}

You can also use macros (#define ...) to avoid the subroutine call and generate inline code instead.
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
yep, thanks

Just for Bhargav's information: The typo is highlighted here in bold font:
return x<<3 + x<<1;
 
Top