fls.c revision 251069
1124483Sdes/*- 2124483Sdes * Copyright (c) 1990, 1993 3124483Sdes * The Regents of the University of California. All rights reserved. 4124483Sdes * 5124483Sdes * Redistribution and use in source and binary forms, with or without 6124483Sdes * modification, are permitted provided that the following conditions 7124483Sdes * are met: 8124483Sdes * 1. Redistributions of source code must retain the above copyright 9124483Sdes * notice, this list of conditions and the following disclaimer. 10124483Sdes * 2. Redistributions in binary form must reproduce the above copyright 11124483Sdes * notice, this list of conditions and the following disclaimer in the 12124483Sdes * documentation and/or other materials provided with the distribution. 13251069Semaste * 3. Neither the name of the University nor the names of its contributors 14124483Sdes * may be used to endorse or promote products derived from this software 15124483Sdes * without specific prior written permission. 16124483Sdes * 17124483Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18124483Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19124483Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20124483Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21124483Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22124483Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23124483Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24124483Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25124483Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26124483Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27124483Sdes * SUCH DAMAGE. 28124483Sdes */ 29124483Sdes 30124483Sdes#include <sys/cdefs.h> 31124483Sdes__FBSDID("$FreeBSD: head/lib/libc/string/fls.c 251069 2013-05-28 20:57:40Z emaste $"); 32124483Sdes 33124483Sdes#include <strings.h> 34124483Sdes 35124483Sdes/* 36124483Sdes * Find Last Set bit 37124483Sdes */ 38124483Sdesint 39124483Sdesfls(int mask) 40124483Sdes{ 41124483Sdes int bit; 42124483Sdes 43124483Sdes if (mask == 0) 44124483Sdes return (0); 45124483Sdes for (bit = 1; mask != 1; bit++) 46124514Sdes mask = (unsigned int)mask >> 1; 47124483Sdes return (bit); 48124483Sdes} 49