Number of the bit

Anton

Member
Join Date
Mar 2003
Location
Israel
Posts
5
May be somebody knows?
In a 16 bit word , only one bit is set ON.
The question to identify the place of this bit in the word.
What is its number in the word?
Thank you.
Anton
 
Last edited:
Number in the word:
Case UINT: 2 powerd by bitnumber
Case INT: If bitnumber<15 as UINT, if binumber is 15 then it is -1

What bit have set ? Debends what you have, PLC or PC and what software you can use for it.
 
Somewher in the ocumentation should be the convention the PLC brand you have uses for bit ID. It varies from brand to brand, ans sometiems even from model to model. Some start left to right, some right to left, some have the LSB (Least Significant Bit) the lower address, some the higher. Some start at bit 0, some at bit 1. That's why you need to identify the specific platform you are using.
 
I've been mulling this over for a few weeks now, trying to come up with an approach that isn't brand-specific. Here's what I've got :

1. Set up a register with the LSB set to 1 and all other bits at 0. We'll call it "Mask"

2. Set a counter with a preset of 16

3. In your logic, test "Mask" against the word you are examining with an AND instruction - I've not yet seen the PLC that cannot do a logical AND.

4. If the test result = 0, increment the counter by 1. Multiply "Mask" by 2, which has the effect of a bit-shift-left instruction.

If the counter reaches 16 (or 15, depending on the numbering format in the PLC being used), reset the counter and mask register to 1 and repeat from step 3.

5. If the test result <> 0 (the value doesn't matter) then the current count in the counter is the place of the bit.

6. If you are looking for the first non-zero bit, store the count value in a register and reset the counter and mask to 1. Repeat. It will always return the position of the first non-zero bit, wherever it may be.

7. If you are looking for the position of all non-zero bits, set up a bank of 16 registers, then write the position into the register corresponding to the counter value.

Good luck!

TM
 
bit number Post #1

May be somebody knows?
In a 16 bit word , only one bit is set ON.
The question to identify the place of this bit in the word.
What is its number in the word?
It means that i have to recieve this number.
Thank you.
Anton

Sorry, i fogot to wright :OMRON PLC (CPU43)

Anton.
 
Just multiply the 16 bit number by the log of 2 (you will have to work that out on a calculator) and provided only one bit is set, Hey Presto, you have the bit number.
 
You must be kidding!

Logs to find bit numbers? Do you have any idea how much processing power that takes if there isn't a floating point unit?

Tim has the right idea on how the algorithm works, but most new PLCs have and ENC or Encode instruction. Also see Decode.

Some PLCs have a different name for this function. RTFM.
Omron has a instruction that can find a bit out of 16 16bit registers.

BTW, on a DSP I convert the integer to float and use the exponent. I have the benefit of floating point.

Tim's method works ok on a processor that can't handle bytes. When I do this I check the whole 32 bits for a bit. Then check to see if the bits are set in the lower or upper 16 bits. Then I check to see if the bit is set in the upper and lower byte. I shift only bytes with bits set. This limits the amount of shifts to 8.

More tidbits...

If you really want to master bit manipulation do a seach on google for "chess bit boards". Chess programmers are the master of bit manipulation. The concept of treating bits as sets will make you think about bits in new ways.

I have watched an older thread on counting bits. Check this out:

bitcount will be the number of bits set in register.
Code:
bitcount = 0;
while ( register <> 0 )
   register = register and ( register - 1 )
   bitcount = bitcount + 1;
end while

Notice that it only loops as many times as there are bits - 1. This is therefore extremely efficient when the bits are few. If there are likely to be more bits than zeros then not the register. Then you will be counts 0s.
 

Similar Topics

In RSLogix500, is there a way to make B3:1/1 to show as B3:0/16? In the below drawing with keyboard letters, --II-- is a rung with a normally...
Replies
6
Views
2,083
I am having problems expressing an ANALOG OUTPUT 16bit INT word (0-10V Proportional Valve Voltage) as a REAL decimal number. From my...
Replies
6
Views
9,167
I am trying to duplicate some Modicon code in a Contrologix PLC. This a sequence step number that is incremented on each step (78 steps). The...
Replies
5
Views
2,335
Since Siemens is big endian, is there a quick and easy way to determine the trigger bit for discrete alarms? I'm looking to implement something...
Replies
2
Views
5,082
Never had this error. It gives me errors on the integervalues (db data)
Replies
0
Views
1,659
Back
Top Bottom