⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitsets.970425.html

📁 this is a mirrored site c-faq. thought might need offline
💻 HTML
字号:
<html><!-- Mirrored from c-faq.com/misc/bitsets.970425.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:51 GMT --><head><title></title></head><body><p>[Someone asked how to implement sets using arrays of bits.This is an excerpt from the additional explanation I posted.]<p>Newsgroups: comp.lang.c<br>From: scs@eskimo.com (Steve Summit)<br>Subject: Re: pascal-like set implementation<br>Message-ID: &lt;E97GrL.F33@eskimo.com&gt;<br>Date: Fri, 25 Apr 1997 18:02:08 GMT<p>If the repertoire of elements which might be contained in the set(what was that buzzword from set theory? ``Universe''?) is known,then the thing to do is set up a correspondence between thoseelements and the 0-based integers.  (That is, if the universe isthe elements ``apple'', ``pear'', and ``orange'', set apple = 0,pear = 1, and orange = 2.  Hmm, you might be able to slickly usean enum for this.)  Then, each set you want to manipulate is anarray of <TT>BITNSLOTS(n)</TT> elements.  To add an element to a set,turn on the corresponding bit.  To remove an element from a set,turn off the corresponding bit.  To compute the union of twosets, run the loop<p><pre>	for(i = 0; i &lt; BITNSLOTS(nb); i++)		set3[i] = set1[i] | set2[i];</pre><p>Computing the intersection is left as an exercise for the reader.<p>Printing a set out will obviously require iterating over all thebits, using <TT>BITTEST()</TT> to discover whether each bit is turned on,and then somehow going from the bit index back to the name of theelement, if necessary.  (We had a thread a week or two ago abouthow to print enum values symbolically.  Here, since the indicesare known to be 0-based integers, a simple lookup table, in theform of an array of strings, would suffice.)<p><address><a href="http://www.eskimo.com/~scs/">Steve Summit</a><br><a href="mailto:scs@eskimo.com">scs@eskimo.com</a></address></body><!-- Mirrored from c-faq.com/misc/bitsets.970425.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:02:51 GMT --></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -