title: Long numbers in PostgreSQL and bitwise operations
body: <p>There's a django application using PostgreSQL DB.<br>
  Our task is to collect and store MSSQL Server permissions (their count is about 120). It implemented as a long number where each bit is 1 permission.<br>
  Thus we need to store a very long number in our PostgreSQL but its MAX numeric value is 64 bits. That's why we store is as a string now. 
  Application creates a SQL query like:</p>
  
  
  
  <pre><code>SELECT "mssql_ace"."id", .... "mssql_ace"."permissions",
  
  FROM "mssql_ace"
  
  LEFT OUTER JOIN ... ON ...
  ...
  WHERE (.... AND ("mssql_ace"."permissions" &amp; 1) = 1)
  
  ORDER BY ... ;
  </code></pre>
  
  <p>The problem now is on WHERE part. We need bitwise operations, it's a way to filter data by permissions that works in the whole app except this. We can't use bitwise operations with string. </p>
  
  <p>We'd tried casting in PostgreSQL: </p>
  
    
  
  <pre><code>1. (CAST("mssql_ace"."permissions" AS BIGINT) &amp; 1)
  
  2. "mssql_ace"."permissions"::BIGINT &amp; 1
  </code></pre>
  
  <p>And other things:</p>
  
  <pre><code>3. DECIMAL also doesn't support bitwise
  </code></pre>
  
  <p>We have an idea to divide this big number on two parts. But it needs a lot of work to change inside app. Any other ideas? The main thing we can't change is usage of bitwise operations.  </p>
  
  <p>Thank you.</p>