---
layout: doc
title: general / next_bit
---
# next_bit

### NAME

    next_bit() - find the next set bit in a bitstring

### SYNOPSIS

    int next_bit( string str, int start );

### DESCRIPTION

    Returns the number of the next set bit in str after start, or -1 if
    there is no later set bit.  The string must be a bitstring of the kind
    produced by set_bit() and clear_bit(), where bits are packed 6 per byte
    in printable characters.

    The search starts after start, not at start.  To iterate over every set
    bit in a bitstring, start with -1:

        int bit = -1;

        while ((bit = next_bit(str, bit)) != -1) {
            /* use bit */
        }

### SEE ALSO

    set_bit(3), clear_bit(3), test_bit(3)
