I have an operator panel with 130 posible bits settings (machine off-on for 130 machines.)
The adresses are from DB10.DBX0.0 to DB10.DBX130.0
I need to know how many machines have been set.
How can I know how many bits are set to one?
I mean, If I have a word, how can i extract the number of bits equal to 1?
[?+]: To count number of bits
-
- Posts: 103
- Joined: Fri Aug 31, 2007 8:17 pm
- Location: Europe
-
- Posts: 15
- Joined: Sun Dec 04, 2005 9:06 am
- Location: Europe
Re: [?]: To count number of bits
Hello,
As i understand your question: the following code counts every true bit number .0
in db10 for the bytes 1 to 130
how it works ? -> see help on stl-statements
OPN DB 10; // your DB 10
L 0;
T MW 200; // zero BIT Counter
L 129; // how many -1 <130>
loop: T MW 100; // store BYTE Counter
SLD 3; // make pointer
LAR1 ; // load pointer in ar1
L DBB [AR1,P#1.0]; // Read bytes down to Byte 1
L 1; // mask for bit 0
AW ; // get it
L MW 200; // load BIT counter
+I ; // count
T MW 200; // store BIT counter
L MW 100; // load Byte counter
LOOP loop; // loop if greater 0
L MW 200; // sum of TRUE bits
be happy,
pwkoller
As i understand your question: the following code counts every true bit number .0
in db10 for the bytes 1 to 130
how it works ? -> see help on stl-statements
OPN DB 10; // your DB 10
L 0;
T MW 200; // zero BIT Counter
L 129; // how many -1 <130>
loop: T MW 100; // store BYTE Counter
SLD 3; // make pointer
LAR1 ; // load pointer in ar1
L DBB [AR1,P#1.0]; // Read bytes down to Byte 1
L 1; // mask for bit 0
AW ; // get it
L MW 200; // load BIT counter
+I ; // count
T MW 200; // store BIT counter
L MW 100; // load Byte counter
LOOP loop; // loop if greater 0
L MW 200; // sum of TRUE bits
be happy,
pwkoller
-
- Posts: 103
- Joined: Fri Aug 31, 2007 8:17 pm
- Location: Europe
Re: [?]: To count number of bits
I'll try it soon
THANKS A LOT
THANKS A LOT
-
- Posts: 103
- Joined: Fri Aug 31, 2007 8:17 pm
- Location: Europe
Re: [?]: To count number of bits
Sorry, it is not working. The program is executing always the loop. MW200 can not be read
-
- Posts: 265
- Joined: Sat May 27, 2006 6:46 am
- Location: Europe
Re: [?]: To count number of bits
hello, 130 bits or 130 byte ?
Your address db10.dbx0.0 to db10.dbx130.0 = 130 x 8 = 1040 bits
please tell me if you want to read all the bit ar only one bit for every byte.
If you want to read one bit for every byte, please indicate the bit number.
in 2-3 days, i send a code to count bit
Your address db10.dbx0.0 to db10.dbx130.0 = 130 x 8 = 1040 bits
please tell me if you want to read all the bit ar only one bit for every byte.
If you want to read one bit for every byte, please indicate the bit number.
in 2-3 days, i send a code to count bit
-
- Posts: 65
- Joined: Thu Nov 01, 2007 9:41 pm
- Location: Belarus
Re: [?]: To count number of bits
another example...
you need to change the bit to the necessary in 2 places (with comments)
Code: Select all
L 0
T MW 0
OPN DB 10
LAR1 P#DBX 0.0
L 130
next: T MW 2
L DBB [AR1,P#0.0]
L 2#100 // for bit 2 (2#00000100). Change here !!!
AW
T MW 4
L MW 0
+I
T MW 0
+AR1 P#1.0
L MW 2
LOOP next
L MW 0
L 2#100 // for bit 2 (2#00000100). Change here !!!
/I
T MW 8
-
- Posts: 103
- Joined: Fri Aug 31, 2007 8:17 pm
- Location: Europe
Re: [?]: To count number of bits
Sorry, I meant 130 bits
The adress 130.0 is obviously wrong. Last one will be around 16.2
I need to know how many bits have been set to one (bit set to one=machine selected). The settings come from an operator panel and there are 130 bits to be set, starting from DBX0.0 of any DB.
Thanks again
The adress 130.0 is obviously wrong. Last one will be around 16.2
I need to know how many bits have been set to one (bit set to one=machine selected). The settings come from an operator panel and there are 130 bits to be set, starting from DBX0.0 of any DB.
Thanks again
-
- Posts: 65
- Joined: Thu Nov 01, 2007 9:41 pm
- Location: Belarus
Re: [?]: To count number of bits
another example...
result in mw6
Code: Select all
L 0
T MW 0
OPN DB 10
LAR1 P#DBX 0.0
L 130
next: T MW 2
A DBX [AR1,P#0.0]
S M 5.0
L MW 0
L MW 4
+I
T MW 0
+AR1 P#0.1
CLR
= M 5.0
L MW 2
LOOP next
NOP 0
L MW 0
T MW 6
-
- Posts: 15
- Joined: Sun Dec 04, 2005 9:06 am
- Location: Europe
Re: [?]: To count number of bits
more for your attention:Sorry, it is not working. The program is executing always the loop. MW200 can not be read
Since the loop makes all its iterations in one cycle you will not see exactly the moment when it is finished
if you place the load mw200 in the next network and just watch this -> you will see the total of bits set.
or you place the code in a fc, call it and then load mw200
or you use the debugger with breakpoints in step7 to go over the loop step bey step -> you will see it works
anyway it runs over 130 bytes from byte 1 counting all the first bits only.
as i see you want to count the bits in an array of bits from 0.0 to 16.1 = 130 therefor the scan-part has to be adjusted
also a part to catch Bit 0.0 has to be added ( its not a good choice to start with data at byte 0 in DBs)
German mnemonic
Code: Select all
AUF DB 10 // your DB 10
L 0
T MW 200 // BIT Counter
L 129 // how many -1 <130>
loop: T MW 100 // store BIT Counter
LAR1 // load pointer in ar1
U DBX [AR1,P#0.0] // Read bits down to Bit 0.1
SPBN NoB // if 0 do not count
L MW 200 // load BIT counter
+ 1 // count
T MW 200 // store BIT counter
NoB: L MW 100 // load Byte counter
LOOP loop // loop if greater 0
LAR1 P#0.0
U DBX [AR1,P#0.0] // Read Bit 0.0
SPBN NoB2 // if 0 do not count
L MW 200 // load BIT counter
+ 1 // count
T MW 200 // store BIT counter
NoB2: NOP 0
L MW 200 // sum of TRUE bits
greetings from pwkoller
-
- Posts: 103
- Joined: Fri Aug 31, 2007 8:17 pm
- Location: Europe
Re: [?]: To count number of bits
Works OK!
Many thanks to both of you
Many thanks to both of you