CyrusBuilt.MonoPi.BitSet.PreviousSetBit C# (CSharp) Method

PreviousSetBit() public method

Returns the index of the nearest bit that is set to true that occurs on or before the specified starting index.
/// cannot be less than zero. ///
public PreviousSetBit ( Int32 fromIndex ) : Int32
fromIndex System.Int32 /// The index to start checking from (inclusive). ///
return System.Int32
		public Int32 PreviousSetBit(Int32 fromIndex) {
			if (fromIndex < 0) {
				if (fromIndex == -1) {
					return -1;
				}
				throw new IndexOutOfRangeException("fromIndex cannot be less than zero.");
			}

			this.CheckInvariants();
			Int32 offset = WordIndex(fromIndex);
			if (offset >= this._wordsInUse) {
				return this.Length - 1;
			}

			Int32 result = -1;
			long w = this._bits[offset] & (LONG_MASK >> -(fromIndex + 1));
			while (true) {
				if (w != 0) {
					result = (offset + 1) * BITS_PER_WORD - 1 - NumberOfTrailingZeros(w);
					break;
				}

				if (offset-- == 0) {
					break;
				}

				w = this._bits[offset];
			}
			return result;
		}