Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As its name implies, <code>SETBIT</code> allows you to perform bit operations - namely set a given bit to 0 or 1, at a given bit offset, for a given key.</p> <p>What is important to understand is that the result not always includes only printable characters. This is why Redis uses a <a href="https://github.com/antirez/redis/blob/320fa02/deps/hiredis/sds.c#L553-L576">custom function <code>sdscatrepr</code></a> to format the CLI output:</p> <blockquote> <p>Append to the sds string "s" an escaped string representation where all the non-printable characters (tested with isprint()) are turned into escapes in the form "\n\r\a...." or "\x".</p> </blockquote> <p>That being said let's start with a simple example. If you consider the hex number <code>0x7F</code> (= 127) its binary representation on 8-bit is:</p> <pre><code>pos: 0 1 2 3 4 5 6 7 bit: 0 1 1 1 1 1 1 1 ^ ^ | | MSB LSB </code></pre> <p>You can typically use <code>SETBIT</code> to store this value, keeping in mind that offset <code>0</code> is <code>MSB</code> and offset 7 is <code>LSB</code>:</p> <pre><code>redis&gt; SETBIT myval 0 0 (integer) 0 redis&gt; SETBIT myval 1 1 (integer) 0 redis&gt; SETBIT myval 2 1 (integer) 0 redis&gt; SETBIT myval 3 1 (integer) 0 redis&gt; SETBIT myval 4 1 (integer) 0 redis&gt; SETBIT myval 5 1 (integer) 0 redis&gt; SETBIT myval 6 1 (integer) 0 redis&gt; SETBIT myval 7 1 (integer) 0 </code></pre> <p>The get your value to inspect if:</p> <pre><code>redis&gt; GET myval "\x7f" </code></pre> <p>Now what happens with multi bytes? Let's say you want to store <code>0x52</code> (= 82) which corresponds to character <code>R</code> in ASCII. The 8-bit representation is <code>01010010</code> with bit positions <code>(8, 9, ..., 15)</code> since we want it to be stored right after the first value:</p> <pre><code>redis&gt; SETBIT myval 8 0 (integer) 0 redis&gt; SETBIT myval 9 1 (integer) 0 redis&gt; SETBIT myval 10 0 (integer) 0 redis&gt; SETBIT myval 11 1 (integer) 0 redis&gt; SETBIT myval 12 0 (integer) 0 redis&gt; SETBIT myval 13 0 (integer) 0 redis&gt; SETBIT myval 14 1 (integer) 0 redis&gt; SETBIT myval 15 0 (integer) 0 </code></pre> <p>And you get:</p> <pre><code>redis&gt; GET myval "\x7fR" </code></pre> <p>Here Redis CLI is able to represent the printable character <code>R</code>.</p> <blockquote> <p>When I store string value 1 and 7 into "mykey"</p> </blockquote> <p>It corresponds to <code>01000001</code> which is equal to 65 and <code>0x41</code> in hex. It corresponds to ASCII character <code>A</code>. So doing:</p> <pre><code>redis&gt; SETBIT mykey 1 1 (integer) 0 redis&gt; SETBIT mykey 7 1 (integer) 0 </code></pre> <p>Gives:</p> <pre><code>redis&gt; GET mykey "A" </code></pre> <blockquote> <p>how the getbit works inside redis?</p> </blockquote> <p>It simply returns the value of the bit at the given position. Here:</p> <pre><code>redis&gt; GETBIT mykey 1 (integer) 1 </code></pre> <p>But bit 0 has not been set (it is 0 by default) thus:</p> <pre><code>redis&gt; GETBIT mykey 0 (integer) 0 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload