View previous topic :: View next topic |
Author |
Message |
saqmaster
Joined: 10 Apr 2004 Posts: 42 Location: United Kingdom
|
Posted: Tue Sep 14, 2004 11:30 am Post subject: Best way to do modulus? |
|
|
Whats the best way do to a simple 'mod 256' on something?
Would this work ok? (with the appropriate variable types)
A = 950 'source value
B = 950 / 256
C = B * 256
D = A - C ' D is final value
Thanks
Stu |
|
Back to top |
|
|
philm
Joined: 14 Jul 2004 Posts: 138 Location: Australia
|
Posted: Tue Sep 14, 2004 12:11 pm Post subject: |
|
|
If the number is base 2 (1, 2, 4, ....256)
then subtract 1 and then logical AND
looks better in binary.
950 = 1110110110
256 = 0100000000
950 = 1110110110
255 = 0011111111
AND= 0010110110 = 182
950 mod 256 = 182 |
|
Back to top |
|
|
DJD XTREEM
Joined: 24 Jun 2004 Posts: 190 Location: 127.0.0.1
|
|
Back to top |
|
|
saqmaster
Joined: 10 Apr 2004 Posts: 42 Location: United Kingdom
|
Posted: Tue Sep 14, 2004 1:11 pm Post subject: |
|
|
Thanks.. Unfortunately its not base two, and 'A' is the sum of four bytes.. so I wont be using integers
On another note.. If i'm reading a high and low byte via serial, would it be best to use Overlay mode to then read this back as a single item?
-Stu |
|
Back to top |
|
|
DJD XTREEM
Joined: 24 Jun 2004 Posts: 190 Location: 127.0.0.1
|
Posted: Tue Sep 14, 2004 1:47 pm Post subject: |
|
|
Well you noted 950 as source value.. That is more than 255 and thus more than a byte..
Thats why i used integers.. _________________ Bascom AVR 1.11.8.8 full version
STK500 Programmer
ISP Programmer |
|
Back to top |
|
|
Frankeman
Joined: 11 Aug 2004 Posts: 948 Location: the Netherlands
|
Posted: Tue Sep 14, 2004 9:54 pm Post subject: |
|
|
Quote: |
Thanks.. Unfortunately its not base two, and 'A' is the sum of four bytes.. so I wont be using integers
|
I don't understand you, the sum of four bytes is maximum 1024 so you must use an integer !
In Bascom you can use: D = A MOD 256 (as philm stated before)
Quote: |
On another note.. If i'm reading a high and low byte via serial, would it be best to use Overlay mode to then read this back as a single item?
|
No, use the statement MAKEINT.
var = MAKEINT(LSB , MSB)
Frank. |
|
Back to top |
|
|
saqmaster
Joined: 10 Apr 2004 Posts: 42 Location: United Kingdom
|
Posted: Tue Sep 14, 2004 9:56 pm Post subject: |
|
|
Er, I meant byte, not integer, my bad
Makeint.. I'll check that out.. Thanks |
|
Back to top |
|
|
oe9vfj
Joined: 17 Jun 2004 Posts: 269 Location: Austria, Hard
|
Posted: Wed Sep 15, 2004 7:53 am Post subject: |
|
|
Hi,
MOD 256 is the LS-Byte of a number (Word, Integer, Long)
So, there are two simple ways for MOD 256
1. Get the LOW-Byte of a Word/Integer/Long
2. Overlay a Byte at the Word/Integer/Long
In following example there are both methods shown:
Code: |
Dim w1 as Word
Dim b1 as Byte
Dim b2 as Byte at W1 overlay
w1 = 950
b1 = low(w1) ' Get the LS-Byte
print w1 ; " " ; b1 ; " " ; b2
|
_________________ regards Josef
DOS - File System for BASCOM-AVR on http://members.aon.at/voegel |
|
Back to top |
|
|
saqmaster
Joined: 10 Apr 2004 Posts: 42 Location: United Kingdom
|
Posted: Wed Sep 15, 2004 8:04 am Post subject: |
|
|
Thanks all for the ideas.. Sorted now! |
|
Back to top |
|
|
|