Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!bbn.com!nic!kira!emily!hartley From: hartley@emily.uvm.edu (Stephen J. Hartley) Newsgroups: comp.lang.c Subject: which bits are set Keywords: bits, set, 32-bit integer Message-ID: <1990Dec12.205114.2376@uvm.edu> Date: 12 Dec 90 20:51:14 GMT Sender: news@nic.NEAR.NET Organization: University of Vermont, Department of Computer Science Lines: 71 Remember the series of articles on counting the number of bits that are set in a 32-bit integer? I have a related problem: determining the position numbers of all the bits that are set in a 32-bit integer. The "brute force" approach follows. Is there a faster (clever) way to do this in C? No, I am not a student trying to get the net to do my homework. Thanks in advance for your help. Send e-mail and I will summarize for the net. Steve Hartley Computer Science faculty Trinity University in San Antonio =============================================================================== #define BIT_SETSIZE 32 typedef unsigned long bit_set; #define NELEM(array) (sizeof(array)/sizeof(array[0])) void which_bits_set(); main() { /* * Some test data. */ static bit_set test[] = { 0x7, 0x40000006, 0x7007, 0xf2f4, 0x1f2001f4, 0x80000000, 0xffffffff, 0xabcabcdd, 0x0, 0x10000000, 0x00070000, 0x11111111, 0x22222222, 0x33333333, 0x44444444 }; int i, m; bit_set set_of_bits; int bits_are_set[BIT_SETSIZE]; for (i=0; i> 1; } if (m < BIT_SETSIZE) bits_are_set[m] = -1; /* terminator if room */ } =============================================================================== Stephen J. Hartley, Assistant Professor, phone O:(512)736-7480, H:344-6523 Department of Computer Science, Trinity University, San Antonio, TX 78212 hartley@uvm.edu || ...!uvm.edu!hartley || ...!uunet!uvm-gen!hartley