View previous topic :: View next topic |
Author |
Message |
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Fri Nov 14, 2014 8:19 pm Post subject: Split Integer into 2 bytes |
|
|
Ok, so why doesn't this work?
Code: |
$regfile = "m128def.dat"
$hwstack = 800
$swstack = 600
$framesize = 100
$crystal = 16000000
$baud = 9600
Dim TempInteger As Integer
Dim TempByte As Byte
Dim TempByte2 As Byte
TempInteger = 1006
TempByte = High(TempInteger)
TempByte2 = Low(TempInteger)
Print Tempbyte ; TempByte2
|
Result is 3238
I was expecting 1006?
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Nov 14, 2014 8:57 pm Post subject: |
|
|
numbers of base 10 system which we use, can not be stored in a computer with base 10 since the computer only knows 0 and 1.
So numbers are binary coded. And we group 8 bits into a byte. For 8 bits, we can represent maximum 255 : 1111_1111 : 128+64+32+16+8+4+2+1
If we want bigger numbers for an integer/word we use 2 bytes. The LSB is still representing 0-255 but the MSB has a different weight : 256+512+1024+2048 etc.
If you use low() you get the LSB. If you use high() you get the MSB. But when printing these bytes, they loose their weight/significance. And you print as a decimal number.
So 3 means 3*256 and 238 need to be added : (3*256) + 238 which should make 1006.
Use the simulator to have a look at the bits and bytes.
consider what you was expecting for values like 32000 and 3271 ? _________________ Mark |
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Fri Nov 14, 2014 10:00 pm Post subject: |
|
|
I didn't take into account the weighting issue when trying to print those values.
My main concern is that an integer with a value of 1006 can be split into 2 bytes with actual values of 10 and 6 respectively. Are you saying low() and high() do that but that can't actually be printed but the bytes do contain the correct values for other uses? |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Nov 14, 2014 10:13 pm Post subject: |
|
|
when you use someInt=1006, it will be assigned with a binary number, the only number that can be used. have a look in the simulator.
1006 is just like we humans want to see a number. when you use INPUT , this decimal number is converted into binary. and when you use PRINT, this binary number is converted into a decimal string.
you can use str() to get a string, and then use left/right/mid or str2digits. _________________ Mark |
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Fri Nov 14, 2014 10:27 pm Post subject: |
|
|
Thanks Mark
I was hoping to have smaller compiled code than using strings but I guess it is what it is...... |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Fri Nov 14, 2014 11:18 pm Post subject: |
|
|
A = I \ 100
B = I MOD 100 |
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Fri Nov 14, 2014 11:36 pm Post subject: |
|
|
MWS wrote: | A = I \ 100
B = I MOD 100 |
Nope
Produces "2" and "38" |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Fri Nov 14, 2014 11:54 pm Post subject: |
|
|
If 1006 divided by 100 gives you 2, you do something seriously wrong. |
|
Back to top |
|
|
albertsm
Joined: 09 Apr 2004 Posts: 5913 Location: Holland
|
Posted: Fri Nov 14, 2014 11:57 pm Post subject: |
|
|
if you want integer division, the assigned variable need to be an integer too. _________________ Mark |
|
Back to top |
|
|
KenHorse
Joined: 16 Jul 2004 Posts: 523
|
Posted: Sat Nov 15, 2014 12:34 am Post subject: |
|
|
albertsm wrote: | if you want integer division, the assigned variable need to be an integer too. |
Indeed
TempInteger2 = TempInteger \ 100
Byte1 = TempInteger2
TempInteger2 = TempInteger MOD 100
Byte2 = TempInteger2
Works just fine (and saves quite a bit of compiled space compared to handling the string equivalent)
Danke! |
|
Back to top |
|
|
|