xref: /redis-3.2.3/src/bitops.c (revision 28343966)
1 /* Bit operations.
2  *
3  * Copyright (c) 2009-2012, Salvatore Sanfilippo <antirez at gmail dot com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *   * Redistributions of source code must retain the above copyright notice,
10  *     this list of conditions and the following disclaimer.
11  *   * Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *   * Neither the name of Redis nor the names of its contributors may be used
15  *     to endorse or promote products derived from this software without
16  *     specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "redis.h"
32 
33 /* -----------------------------------------------------------------------------
34  * Helpers and low level bit functions.
35  * -------------------------------------------------------------------------- */
36 
37 /* This helper function used by GETBIT / SETBIT parses the bit offset argument
38  * making sure an error is returned if it is negative or if it overflows
39  * Redis 512 MB limit for the string value. */
40 static int getBitOffsetFromArgument(redisClient *c, robj *o, size_t *offset) {
41     long long loffset;
42     char *err = "bit offset is not an integer or out of range";
43 
44     if (getLongLongFromObjectOrReply(c,o,&loffset,err) != REDIS_OK)
45         return REDIS_ERR;
46 
47     /* Limit offset to 512MB in bytes */
48     if ((loffset < 0) || ((unsigned long long)loffset >> 3) >= (512*1024*1024))
49     {
50         addReplyError(c,err);
51         return REDIS_ERR;
52     }
53 
54     *offset = (size_t)loffset;
55     return REDIS_OK;
56 }
57 
58 /* Count number of bits set in the binary array pointed by 's' and long
59  * 'count' bytes. The implementation of this function is required to
60  * work with a input string length up to 512 MB. */
61 size_t redisPopcount(void *s, long count) {
62     size_t bits = 0;
63     unsigned char *p = s;
64     uint32_t *p4;
65     static const unsigned char bitsinbyte[256] = {0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5,6,5,6,6,7,5,6,6,7,6,7,7,8};
66 
67     /* Count initial bytes not aligned to 32 bit. */
68     while((unsigned long)p & 3 && count) {
69         bits += bitsinbyte[*p++];
70         count--;
71     }
72 
73     /* Count bits 16 bytes at a time */
74     p4 = (uint32_t*)p;
75     while(count>=16) {
76         uint32_t aux1, aux2, aux3, aux4;
77 
78         aux1 = *p4++;
79         aux2 = *p4++;
80         aux3 = *p4++;
81         aux4 = *p4++;
82         count -= 16;
83 
84         aux1 = aux1 - ((aux1 >> 1) & 0x55555555);
85         aux1 = (aux1 & 0x33333333) + ((aux1 >> 2) & 0x33333333);
86         aux2 = aux2 - ((aux2 >> 1) & 0x55555555);
87         aux2 = (aux2 & 0x33333333) + ((aux2 >> 2) & 0x33333333);
88         aux3 = aux3 - ((aux3 >> 1) & 0x55555555);
89         aux3 = (aux3 & 0x33333333) + ((aux3 >> 2) & 0x33333333);
90         aux4 = aux4 - ((aux4 >> 1) & 0x55555555);
91         aux4 = (aux4 & 0x33333333) + ((aux4 >> 2) & 0x33333333);
92         bits += ((((aux1 + (aux1 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24) +
93                 ((((aux2 + (aux2 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24) +
94                 ((((aux3 + (aux3 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24) +
95                 ((((aux4 + (aux4 >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24);
96     }
97     /* Count the remaining bytes. */
98     p = (unsigned char*)p4;
99     while(count--) bits += bitsinbyte[*p++];
100     return bits;
101 }
102 
103 /* Return the position of the first bit set to one (if 'bit' is 1) or
104  * zero (if 'bit' is 0) in the bitmap starting at 's' and long 'count' bytes.
105  *
106  * The function is guaranteed to return a value >= 0 if 'bit' is 0 since if
107  * no zero bit is found, it returns count*8 assuming the string is zero
108  * padded on the right. However if 'bit' is 1 it is possible that there is
109  * not a single set bit in the bitmap. In this special case -1 is returned. */
110 long redisBitpos(void *s, unsigned long count, int bit) {
111     unsigned long *l;
112     unsigned char *c;
113     unsigned long skipval, word = 0, one;
114     long pos = 0; /* Position of bit, to return to the caller. */
115     unsigned long j;
116 
117     /* Process whole words first, seeking for first word that is not
118      * all ones or all zeros respectively if we are lookig for zeros
119      * or ones. This is much faster with large strings having contiguous
120      * blocks of 1 or 0 bits compared to the vanilla bit per bit processing.
121      *
122      * Note that if we start from an address that is not aligned
123      * to sizeof(unsigned long) we consume it byte by byte until it is
124      * aligned. */
125 
126     /* Skip initial bits not aligned to sizeof(unsigned long) byte by byte. */
127     skipval = bit ? 0 : UCHAR_MAX;
128     c = (unsigned char*) s;
129     while((unsigned long)c & (sizeof(*l)-1) && count) {
130         if (*c != skipval) break;
131         c++;
132         count--;
133         pos += 8;
134     }
135 
136     /* Skip bits with full word step. */
137     skipval = bit ? 0 : ULONG_MAX;
138     l = (unsigned long*) c;
139     while (count >= sizeof(*l)) {
140         if (*l != skipval) break;
141         l++;
142         count -= sizeof(*l);
143         pos += sizeof(*l)*8;
144     }
145 
146     /* Load bytes into "word" considering the first byte as the most significant
147      * (we basically consider it as written in big endian, since we consider the
148      * string as a set of bits from left to right, with the first bit at position
149      * zero.
150      *
151      * Note that the loading is designed to work even when the bytes left
152      * (count) are less than a full word. We pad it with zero on the right. */
153     c = (unsigned char*)l;
154     for (j = 0; j < sizeof(*l); j++) {
155         word <<= 8;
156         if (count) {
157             word |= *c;
158             c++;
159             count--;
160         }
161     }
162 
163     /* Special case:
164      * If bits in the string are all zero and we are looking for one,
165      * return -1 to signal that there is not a single "1" in the whole
166      * string. This can't happen when we are looking for "0" as we assume
167      * that the right of the string is zero padded. */
168     if (bit == 1 && word == 0) return -1;
169 
170     /* Last word left, scan bit by bit. The first thing we need is to
171      * have a single "1" set in the most significant position in an
172      * unsigned long. We don't know the size of the long so we use a
173      * simple trick. */
174     one = ULONG_MAX; /* All bits set to 1.*/
175     one >>= 1;       /* All bits set to 1 but the MSB. */
176     one = ~one;      /* All bits set to 0 but the MSB. */
177 
178     while(one) {
179         if (((one & word) != 0) == bit) return pos;
180         pos++;
181         one >>= 1;
182     }
183 
184     /* If we reached this point, there is a bug in the algorithm, since
185      * the case of no match is handled as a special case before. */
186     redisPanic("End of redisBitpos() reached.");
187     return 0; /* Just to avoid warnings. */
188 }
189 
190 /* -----------------------------------------------------------------------------
191  * Bits related string commands: GETBIT, SETBIT, BITCOUNT, BITOP.
192  * -------------------------------------------------------------------------- */
193 
194 #define BITOP_AND   0
195 #define BITOP_OR    1
196 #define BITOP_XOR   2
197 #define BITOP_NOT   3
198 
199 /* SETBIT key offset bitvalue */
200 void setbitCommand(redisClient *c) {
201     robj *o;
202     char *err = "bit is not an integer or out of range";
203     size_t bitoffset;
204     int byte, bit;
205     int byteval, bitval;
206     long on;
207 
208     if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset) != REDIS_OK)
209         return;
210 
211     if (getLongFromObjectOrReply(c,c->argv[3],&on,err) != REDIS_OK)
212         return;
213 
214     /* Bits can only be set or cleared... */
215     if (on & ~1) {
216         addReplyError(c,err);
217         return;
218     }
219 
220     o = lookupKeyWrite(c->db,c->argv[1]);
221     if (o == NULL) {
222         o = createObject(REDIS_STRING,sdsempty());
223         dbAdd(c->db,c->argv[1],o);
224     } else {
225         if (checkType(c,o,REDIS_STRING)) return;
226         o = dbUnshareStringValue(c->db,c->argv[1],o);
227     }
228 
229     /* Grow sds value to the right length if necessary */
230     byte = bitoffset >> 3;
231     o->ptr = sdsgrowzero(o->ptr,byte+1);
232 
233     /* Get current values */
234     byteval = ((uint8_t*)o->ptr)[byte];
235     bit = 7 - (bitoffset & 0x7);
236     bitval = byteval & (1 << bit);
237 
238     /* Update byte with new bit value and return original value */
239     byteval &= ~(1 << bit);
240     byteval |= ((on & 0x1) << bit);
241     ((uint8_t*)o->ptr)[byte] = byteval;
242     signalModifiedKey(c->db,c->argv[1]);
243     notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"setbit",c->argv[1],c->db->id);
244     server.dirty++;
245     addReply(c, bitval ? shared.cone : shared.czero);
246 }
247 
248 /* GETBIT key offset */
249 void getbitCommand(redisClient *c) {
250     robj *o;
251     char llbuf[32];
252     size_t bitoffset;
253     size_t byte, bit;
254     size_t bitval = 0;
255 
256     if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset) != REDIS_OK)
257         return;
258 
259     if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
260         checkType(c,o,REDIS_STRING)) return;
261 
262     byte = bitoffset >> 3;
263     bit = 7 - (bitoffset & 0x7);
264     if (sdsEncodedObject(o)) {
265         if (byte < sdslen(o->ptr))
266             bitval = ((uint8_t*)o->ptr)[byte] & (1 << bit);
267     } else {
268         if (byte < (size_t)ll2string(llbuf,sizeof(llbuf),(long)o->ptr))
269             bitval = llbuf[byte] & (1 << bit);
270     }
271 
272     addReply(c, bitval ? shared.cone : shared.czero);
273 }
274 
275 /* BITOP op_name target_key src_key1 src_key2 src_key3 ... src_keyN */
276 void bitopCommand(redisClient *c) {
277     char *opname = c->argv[1]->ptr;
278     robj *o, *targetkey = c->argv[2];
279     unsigned long op, j, numkeys;
280     robj **objects;      /* Array of source objects. */
281     unsigned char **src; /* Array of source strings pointers. */
282     unsigned long *len, maxlen = 0; /* Array of length of src strings,
283                                        and max len. */
284     unsigned long minlen = 0;    /* Min len among the input keys. */
285     unsigned char *res = NULL; /* Resulting string. */
286 
287     /* Parse the operation name. */
288     if ((opname[0] == 'a' || opname[0] == 'A') && !strcasecmp(opname,"and"))
289         op = BITOP_AND;
290     else if((opname[0] == 'o' || opname[0] == 'O') && !strcasecmp(opname,"or"))
291         op = BITOP_OR;
292     else if((opname[0] == 'x' || opname[0] == 'X') && !strcasecmp(opname,"xor"))
293         op = BITOP_XOR;
294     else if((opname[0] == 'n' || opname[0] == 'N') && !strcasecmp(opname,"not"))
295         op = BITOP_NOT;
296     else {
297         addReply(c,shared.syntaxerr);
298         return;
299     }
300 
301     /* Sanity check: NOT accepts only a single key argument. */
302     if (op == BITOP_NOT && c->argc != 4) {
303         addReplyError(c,"BITOP NOT must be called with a single source key.");
304         return;
305     }
306 
307     /* Lookup keys, and store pointers to the string objects into an array. */
308     numkeys = c->argc - 3;
309     src = zmalloc(sizeof(unsigned char*) * numkeys);
310     len = zmalloc(sizeof(long) * numkeys);
311     objects = zmalloc(sizeof(robj*) * numkeys);
312     for (j = 0; j < numkeys; j++) {
313         o = lookupKeyRead(c->db,c->argv[j+3]);
314         /* Handle non-existing keys as empty strings. */
315         if (o == NULL) {
316             objects[j] = NULL;
317             src[j] = NULL;
318             len[j] = 0;
319             minlen = 0;
320             continue;
321         }
322         /* Return an error if one of the keys is not a string. */
323         if (checkType(c,o,REDIS_STRING)) {
324             unsigned long i;
325             for (i = 0; i < j; i++) {
326                 if (objects[i])
327                     decrRefCount(objects[i]);
328             }
329             zfree(src);
330             zfree(len);
331             zfree(objects);
332             return;
333         }
334         objects[j] = getDecodedObject(o);
335         src[j] = objects[j]->ptr;
336         len[j] = sdslen(objects[j]->ptr);
337         if (len[j] > maxlen) maxlen = len[j];
338         if (j == 0 || len[j] < minlen) minlen = len[j];
339     }
340 
341     /* Compute the bit operation, if at least one string is not empty. */
342     if (maxlen) {
343         res = (unsigned char*) sdsnewlen(NULL,maxlen);
344         unsigned char output, byte;
345         unsigned long i;
346 
347         /* Fast path: as far as we have data for all the input bitmaps we
348          * can take a fast path that performs much better than the
349          * vanilla algorithm. */
350         j = 0;
351         if (minlen && numkeys <= 16) {
352             unsigned long *lp[16];
353             unsigned long *lres = (unsigned long*) res;
354 
355             /* Note: sds pointer is always aligned to 8 byte boundary. */
356             memcpy(lp,src,sizeof(unsigned long*)*numkeys);
357             memcpy(res,src[0],minlen);
358 
359             /* Different branches per different operations for speed (sorry). */
360             if (op == BITOP_AND) {
361                 while(minlen >= sizeof(unsigned long)*4) {
362                     for (i = 1; i < numkeys; i++) {
363                         lres[0] &= lp[i][0];
364                         lres[1] &= lp[i][1];
365                         lres[2] &= lp[i][2];
366                         lres[3] &= lp[i][3];
367                         lp[i]+=4;
368                     }
369                     lres+=4;
370                     j += sizeof(unsigned long)*4;
371                     minlen -= sizeof(unsigned long)*4;
372                 }
373             } else if (op == BITOP_OR) {
374                 while(minlen >= sizeof(unsigned long)*4) {
375                     for (i = 1; i < numkeys; i++) {
376                         lres[0] |= lp[i][0];
377                         lres[1] |= lp[i][1];
378                         lres[2] |= lp[i][2];
379                         lres[3] |= lp[i][3];
380                         lp[i]+=4;
381                     }
382                     lres+=4;
383                     j += sizeof(unsigned long)*4;
384                     minlen -= sizeof(unsigned long)*4;
385                 }
386             } else if (op == BITOP_XOR) {
387                 while(minlen >= sizeof(unsigned long)*4) {
388                     for (i = 1; i < numkeys; i++) {
389                         lres[0] ^= lp[i][0];
390                         lres[1] ^= lp[i][1];
391                         lres[2] ^= lp[i][2];
392                         lres[3] ^= lp[i][3];
393                         lp[i]+=4;
394                     }
395                     lres+=4;
396                     j += sizeof(unsigned long)*4;
397                     minlen -= sizeof(unsigned long)*4;
398                 }
399             } else if (op == BITOP_NOT) {
400                 while(minlen >= sizeof(unsigned long)*4) {
401                     lres[0] = ~lres[0];
402                     lres[1] = ~lres[1];
403                     lres[2] = ~lres[2];
404                     lres[3] = ~lres[3];
405                     lres+=4;
406                     j += sizeof(unsigned long)*4;
407                     minlen -= sizeof(unsigned long)*4;
408                 }
409             }
410         }
411 
412         /* j is set to the next byte to process by the previous loop. */
413         for (; j < maxlen; j++) {
414             output = (len[0] <= j) ? 0 : src[0][j];
415             if (op == BITOP_NOT) output = ~output;
416             for (i = 1; i < numkeys; i++) {
417                 byte = (len[i] <= j) ? 0 : src[i][j];
418                 switch(op) {
419                 case BITOP_AND: output &= byte; break;
420                 case BITOP_OR:  output |= byte; break;
421                 case BITOP_XOR: output ^= byte; break;
422                 }
423             }
424             res[j] = output;
425         }
426     }
427     for (j = 0; j < numkeys; j++) {
428         if (objects[j])
429             decrRefCount(objects[j]);
430     }
431     zfree(src);
432     zfree(len);
433     zfree(objects);
434 
435     /* Store the computed value into the target key */
436     if (maxlen) {
437         o = createObject(REDIS_STRING,res);
438         setKey(c->db,targetkey,o);
439         notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"set",targetkey,c->db->id);
440         decrRefCount(o);
441     } else if (dbDelete(c->db,targetkey)) {
442         signalModifiedKey(c->db,targetkey);
443         notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",targetkey,c->db->id);
444     }
445     server.dirty++;
446     addReplyLongLong(c,maxlen); /* Return the output string length in bytes. */
447 }
448 
449 /* BITCOUNT key [start end] */
450 void bitcountCommand(redisClient *c) {
451     robj *o;
452     long start, end, strlen;
453     unsigned char *p;
454     char llbuf[32];
455 
456     /* Lookup, check for type, and return 0 for non existing keys. */
457     if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
458         checkType(c,o,REDIS_STRING)) return;
459 
460     /* Set the 'p' pointer to the string, that can be just a stack allocated
461      * array if our string was integer encoded. */
462     if (o->encoding == REDIS_ENCODING_INT) {
463         p = (unsigned char*) llbuf;
464         strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr);
465     } else {
466         p = (unsigned char*) o->ptr;
467         strlen = sdslen(o->ptr);
468     }
469 
470     /* Parse start/end range if any. */
471     if (c->argc == 4) {
472         if (getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != REDIS_OK)
473             return;
474         if (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != REDIS_OK)
475             return;
476         /* Convert negative indexes */
477         if (start < 0) start = strlen+start;
478         if (end < 0) end = strlen+end;
479         if (start < 0) start = 0;
480         if (end < 0) end = 0;
481         if (end >= strlen) end = strlen-1;
482     } else if (c->argc == 2) {
483         /* The whole string. */
484         start = 0;
485         end = strlen-1;
486     } else {
487         /* Syntax error. */
488         addReply(c,shared.syntaxerr);
489         return;
490     }
491 
492     /* Precondition: end >= 0 && end < strlen, so the only condition where
493      * zero can be returned is: start > end. */
494     if (start > end) {
495         addReply(c,shared.czero);
496     } else {
497         long bytes = end-start+1;
498 
499         addReplyLongLong(c,redisPopcount(p+start,bytes));
500     }
501 }
502 
503 /* BITPOS key bit [start [end]] */
504 void bitposCommand(redisClient *c) {
505     robj *o;
506     long bit, start, end, strlen;
507     unsigned char *p;
508     char llbuf[32];
509     int end_given = 0;
510 
511     /* Parse the bit argument to understand what we are looking for, set
512      * or clear bits. */
513     if (getLongFromObjectOrReply(c,c->argv[2],&bit,NULL) != REDIS_OK)
514         return;
515     if (bit != 0 && bit != 1) {
516         addReplyError(c, "The bit argument must be 1 or 0.");
517         return;
518     }
519 
520     /* If the key does not exist, from our point of view it is an infinite
521      * array of 0 bits. If the user is looking for the fist clear bit return 0,
522      * If the user is looking for the first set bit, return -1. */
523     if ((o = lookupKeyRead(c->db,c->argv[1])) == NULL) {
524         addReplyLongLong(c, bit ? -1 : 0);
525         return;
526     }
527     if (checkType(c,o,REDIS_STRING)) return;
528 
529     /* Set the 'p' pointer to the string, that can be just a stack allocated
530      * array if our string was integer encoded. */
531     if (o->encoding == REDIS_ENCODING_INT) {
532         p = (unsigned char*) llbuf;
533         strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr);
534     } else {
535         p = (unsigned char*) o->ptr;
536         strlen = sdslen(o->ptr);
537     }
538 
539     /* Parse start/end range if any. */
540     if (c->argc == 4 || c->argc == 5) {
541         if (getLongFromObjectOrReply(c,c->argv[3],&start,NULL) != REDIS_OK)
542             return;
543         if (c->argc == 5) {
544             if (getLongFromObjectOrReply(c,c->argv[4],&end,NULL) != REDIS_OK)
545                 return;
546             end_given = 1;
547         } else {
548             end = strlen-1;
549         }
550         /* Convert negative indexes */
551         if (start < 0) start = strlen+start;
552         if (end < 0) end = strlen+end;
553         if (start < 0) start = 0;
554         if (end < 0) end = 0;
555         if (end >= strlen) end = strlen-1;
556     } else if (c->argc == 3) {
557         /* The whole string. */
558         start = 0;
559         end = strlen-1;
560     } else {
561         /* Syntax error. */
562         addReply(c,shared.syntaxerr);
563         return;
564     }
565 
566     /* For empty ranges (start > end) we return -1 as an empty range does
567      * not contain a 0 nor a 1. */
568     if (start > end) {
569         addReplyLongLong(c, -1);
570     } else {
571         long bytes = end-start+1;
572         long pos = redisBitpos(p+start,bytes,bit);
573 
574         /* If we are looking for clear bits, and the user specified an exact
575          * range with start-end, we can't consider the right of the range as
576          * zero padded (as we do when no explicit end is given).
577          *
578          * So if redisBitpos() returns the first bit outside the range,
579          * we return -1 to the caller, to mean, in the specified range there
580          * is not a single "0" bit. */
581         if (end_given && bit == 0 && pos == bytes*8) {
582             addReplyLongLong(c,-1);
583             return;
584         }
585         if (pos != -1) pos += start*8; /* Adjust for the bytes we skipped. */
586         addReplyLongLong(c,pos);
587     }
588 }
589