<br><br> <br><br><div class="gmail_quote">On Tue, Jan 27, 2009 at 14:26, Ricardo Aráoz <span dir="ltr">&lt;<a href="mailto:ricaraoz@gmail.com">ricaraoz@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">



  

<div bgcolor="#ffffff" text="#000000"><div class="Ih2E3d">
<br>
<blockquote type="cite">&quot;Vicent&quot;
<a href="mailto:vginer@gmail.com" target="_blank">&lt;vginer@gmail.com&gt;</a> wrote
  <br>
  <br>
  <blockquote type="cite">Anyway, I am working with Python 2.5.4, and I
am interested in defining a
    <br>
new type called &quot;bit&quot; (if possible), which represents a number that can
only
    <br>
take values 0 or 1 —that&#39;s what we would call a &quot;binary variable&quot;, in a
    <br>
Mathematical Programming context.
    <br>
  </blockquote>
</blockquote></div>
The python manual is usually a good thing to read :<br>
<br>
<h2><a name="11f1f27bbf4c315c_SECTION005410000000000000000"></a><a name="11f1f27bbf4c315c_bitstring-ops"></a><br>
3.4.1 Bit-string Operations on Integer Types </h2>
<a name="11f1f27bbf4c315c_Bit-string_Operations"></a>
<p>Plain and long integer types support additional operations that make
sense only for bit-strings. Negative numbers are treated as their 2&#39;s
complement value (for long integers, this assumes a sufficiently large
number of bits that no overflow occurs during the operation). </p>
<p>The priorities of the binary bit-wise operations are all lower than
the numeric operations and higher than the comparisons; the unary
operation &quot;<tt>~</tt>&quot; has the same priority as the other
unary numeric operations (&quot;<tt>+</tt>&quot; and &quot;<tt>-</tt>&quot;). </p>
<p>This table lists the bit-string operations sorted in ascending
priority (operations in the same box have the same priority): </p>
<p></p>
<div>
<table>
  <thead>
  <tr>
    <th>Operation</th>
    <th>Result</th>
    <th>Notes</th>
  </tr>
  </thead><tbody>
    <tr>
      <td valign="baseline"><code><var>x</var> | <var>y</var></code></td>
      <td>bitwise <i>or</i> of <var>x</var>
and <var>y</var></td>
      <td><br>
      </td>
    </tr>
    <tr>
      <td valign="baseline"><code><var>x</var> ^ <var>y</var></code></td>
      <td>bitwise <i>exclusive or</i> of <var>x</var>
and <var>y</var></td>
      <td><br>
      </td>
    </tr>
    <tr>
      <td valign="baseline"><code><var>x</var> &amp; <var>y</var></code></td>
      <td>bitwise <i>and</i> of <var>x</var>
and <var>y</var></td>
      <td><br>
      </td>
    </tr>
    <tr>
      <td valign="baseline"><code><var>x</var> &lt;&lt; <var>n</var></code></td>
      <td><var>x</var> shifted left by <var>n</var> bits</td>
      <td>(1), (2)</td>
    </tr>
    <tr>
      <td valign="baseline"><code><var>x</var> &gt;&gt; <var>n</var></code></td>
      <td><var>x</var> shifted right by <var>n</var> bits</td>
      <td>(1), (3)</td>
    </tr>
    <tr>
      <td valign="baseline"><code>~<var>x</var></code></td>
      <td>the bits of <var>x</var> inverted</td>
      <td><br>
      </td>
    </tr>
  </tbody>
</table>
</div>
<p>Notes: </p>
<dl>
  <dt><b>(1)</b> </dt>
  <dd>Negative shift counts are illegal and cause a <tt>ValueError</tt> to be raised. </dd>
  <dt><b>(2)</b> </dt>
  <dd>A left shift by <var>n</var> bits is equivalent to
multiplication by <code>pow(2, <var>n</var>)</code> without overflow
check. </dd>
  <dt><b>(3)</b> </dt>
  <dd>A right shift by <var>n</var> bits is equivalent to division by <code>pow(2,
    <var>n</var>)</code> without overflow check. </dd></dl></div></blockquote><div><br>Ricardo,<br><br>Thank you. I think I already read this, but I didn&#39;t realize that it could be useful for me.<br><br>I mean, I was looking for a class that stores only 0-1 values and that is 1-bit-sized, in order to save memory and/or disk space. That&#39;s why I thought that &quot;bool&quot; type would be OK for me, but maybe I could also consider integers (or translating those data into integers), because of those operations that are already implemented and that you have just showed me. Anyway, I think similar operators already exist for single &quot;bool&quot; data.<br>
<br>That approach you suggest would be useful in case I wanted to manage with strings of 0&#39;s and 1&#39;s (which could be possible).<br></div></div><br>Thank you!<br><br clear="all"><br>-- <br>Vicent<br>