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, 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 int 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 227 /* Create a copy when the object is shared or encoded. */ 228 if (o->refcount != 1 || o->encoding != REDIS_ENCODING_RAW) { 229 robj *decoded = getDecodedObject(o); 230 o = createRawStringObject(decoded->ptr, sdslen(decoded->ptr)); 231 decrRefCount(decoded); 232 dbOverwrite(c->db,c->argv[1],o); 233 } 234 } 235 236 /* Grow sds value to the right length if necessary */ 237 byte = bitoffset >> 3; 238 o->ptr = sdsgrowzero(o->ptr,byte+1); 239 240 /* Get current values */ 241 byteval = ((uint8_t*)o->ptr)[byte]; 242 bit = 7 - (bitoffset & 0x7); 243 bitval = byteval & (1 << bit); 244 245 /* Update byte with new bit value and return original value */ 246 byteval &= ~(1 << bit); 247 byteval |= ((on & 0x1) << bit); 248 ((uint8_t*)o->ptr)[byte] = byteval; 249 signalModifiedKey(c->db,c->argv[1]); 250 notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"setbit",c->argv[1],c->db->id); 251 server.dirty++; 252 addReply(c, bitval ? shared.cone : shared.czero); 253 } 254 255 /* GETBIT key offset */ 256 void getbitCommand(redisClient *c) { 257 robj *o; 258 char llbuf[32]; 259 size_t bitoffset; 260 size_t byte, bit; 261 size_t bitval = 0; 262 263 if (getBitOffsetFromArgument(c,c->argv[2],&bitoffset) != REDIS_OK) 264 return; 265 266 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL || 267 checkType(c,o,REDIS_STRING)) return; 268 269 byte = bitoffset >> 3; 270 bit = 7 - (bitoffset & 0x7); 271 if (sdsEncodedObject(o)) { 272 if (byte < sdslen(o->ptr)) 273 bitval = ((uint8_t*)o->ptr)[byte] & (1 << bit); 274 } else { 275 if (byte < (size_t)ll2string(llbuf,sizeof(llbuf),(long)o->ptr)) 276 bitval = llbuf[byte] & (1 << bit); 277 } 278 279 addReply(c, bitval ? shared.cone : shared.czero); 280 } 281 282 /* BITOP op_name target_key src_key1 src_key2 src_key3 ... src_keyN */ 283 void bitopCommand(redisClient *c) { 284 char *opname = c->argv[1]->ptr; 285 robj *o, *targetkey = c->argv[2]; 286 long op, j, numkeys; 287 robj **objects; /* Array of source objects. */ 288 unsigned char **src; /* Array of source strings pointers. */ 289 long *len, maxlen = 0; /* Array of length of src strings, and max len. */ 290 long minlen = 0; /* Min len among the input keys. */ 291 unsigned char *res = NULL; /* Resulting string. */ 292 293 /* Parse the operation name. */ 294 if ((opname[0] == 'a' || opname[0] == 'A') && !strcasecmp(opname,"and")) 295 op = BITOP_AND; 296 else if((opname[0] == 'o' || opname[0] == 'O') && !strcasecmp(opname,"or")) 297 op = BITOP_OR; 298 else if((opname[0] == 'x' || opname[0] == 'X') && !strcasecmp(opname,"xor")) 299 op = BITOP_XOR; 300 else if((opname[0] == 'n' || opname[0] == 'N') && !strcasecmp(opname,"not")) 301 op = BITOP_NOT; 302 else { 303 addReply(c,shared.syntaxerr); 304 return; 305 } 306 307 /* Sanity check: NOT accepts only a single key argument. */ 308 if (op == BITOP_NOT && c->argc != 4) { 309 addReplyError(c,"BITOP NOT must be called with a single source key."); 310 return; 311 } 312 313 /* Lookup keys, and store pointers to the string objects into an array. */ 314 numkeys = c->argc - 3; 315 src = zmalloc(sizeof(unsigned char*) * numkeys); 316 len = zmalloc(sizeof(long) * numkeys); 317 objects = zmalloc(sizeof(robj*) * numkeys); 318 for (j = 0; j < numkeys; j++) { 319 o = lookupKeyRead(c->db,c->argv[j+3]); 320 /* Handle non-existing keys as empty strings. */ 321 if (o == NULL) { 322 objects[j] = NULL; 323 src[j] = NULL; 324 len[j] = 0; 325 minlen = 0; 326 continue; 327 } 328 /* Return an error if one of the keys is not a string. */ 329 if (checkType(c,o,REDIS_STRING)) { 330 for (j = j-1; j >= 0; j--) { 331 if (objects[j]) 332 decrRefCount(objects[j]); 333 } 334 zfree(src); 335 zfree(len); 336 zfree(objects); 337 return; 338 } 339 objects[j] = getDecodedObject(o); 340 src[j] = objects[j]->ptr; 341 len[j] = sdslen(objects[j]->ptr); 342 if (len[j] > maxlen) maxlen = len[j]; 343 if (j == 0 || len[j] < minlen) minlen = len[j]; 344 } 345 346 /* Compute the bit operation, if at least one string is not empty. */ 347 if (maxlen) { 348 res = (unsigned char*) sdsnewlen(NULL,maxlen); 349 unsigned char output, byte; 350 long i; 351 352 /* Fast path: as far as we have data for all the input bitmaps we 353 * can take a fast path that performs much better than the 354 * vanilla algorithm. */ 355 j = 0; 356 if (minlen && numkeys <= 16) { 357 unsigned long *lp[16]; 358 unsigned long *lres = (unsigned long*) res; 359 360 /* Note: sds pointer is always aligned to 8 byte boundary. */ 361 memcpy(lp,src,sizeof(unsigned long*)*numkeys); 362 memcpy(res,src[0],minlen); 363 364 /* Different branches per different operations for speed (sorry). */ 365 if (op == BITOP_AND) { 366 while(minlen >= sizeof(unsigned long)*4) { 367 for (i = 1; i < numkeys; i++) { 368 lres[0] &= lp[i][0]; 369 lres[1] &= lp[i][1]; 370 lres[2] &= lp[i][2]; 371 lres[3] &= lp[i][3]; 372 lp[i]+=4; 373 } 374 lres+=4; 375 j += sizeof(unsigned long)*4; 376 minlen -= sizeof(unsigned long)*4; 377 } 378 } else if (op == BITOP_OR) { 379 while(minlen >= sizeof(unsigned long)*4) { 380 for (i = 1; i < numkeys; i++) { 381 lres[0] |= lp[i][0]; 382 lres[1] |= lp[i][1]; 383 lres[2] |= lp[i][2]; 384 lres[3] |= lp[i][3]; 385 lp[i]+=4; 386 } 387 lres+=4; 388 j += sizeof(unsigned long)*4; 389 minlen -= sizeof(unsigned long)*4; 390 } 391 } else if (op == BITOP_XOR) { 392 while(minlen >= sizeof(unsigned long)*4) { 393 for (i = 1; i < numkeys; i++) { 394 lres[0] ^= lp[i][0]; 395 lres[1] ^= lp[i][1]; 396 lres[2] ^= lp[i][2]; 397 lres[3] ^= lp[i][3]; 398 lp[i]+=4; 399 } 400 lres+=4; 401 j += sizeof(unsigned long)*4; 402 minlen -= sizeof(unsigned long)*4; 403 } 404 } else if (op == BITOP_NOT) { 405 while(minlen >= sizeof(unsigned long)*4) { 406 lres[0] = ~lres[0]; 407 lres[1] = ~lres[1]; 408 lres[2] = ~lres[2]; 409 lres[3] = ~lres[3]; 410 lres+=4; 411 j += sizeof(unsigned long)*4; 412 minlen -= sizeof(unsigned long)*4; 413 } 414 } 415 } 416 417 /* j is set to the next byte to process by the previous loop. */ 418 for (; j < maxlen; j++) { 419 output = (len[0] <= j) ? 0 : src[0][j]; 420 if (op == BITOP_NOT) output = ~output; 421 for (i = 1; i < numkeys; i++) { 422 byte = (len[i] <= j) ? 0 : src[i][j]; 423 switch(op) { 424 case BITOP_AND: output &= byte; break; 425 case BITOP_OR: output |= byte; break; 426 case BITOP_XOR: output ^= byte; break; 427 } 428 } 429 res[j] = output; 430 } 431 } 432 for (j = 0; j < numkeys; j++) { 433 if (objects[j]) 434 decrRefCount(objects[j]); 435 } 436 zfree(src); 437 zfree(len); 438 zfree(objects); 439 440 /* Store the computed value into the target key */ 441 if (maxlen) { 442 o = createObject(REDIS_STRING,res); 443 setKey(c->db,targetkey,o); 444 notifyKeyspaceEvent(REDIS_NOTIFY_STRING,"set",targetkey,c->db->id); 445 decrRefCount(o); 446 } else if (dbDelete(c->db,targetkey)) { 447 signalModifiedKey(c->db,targetkey); 448 notifyKeyspaceEvent(REDIS_NOTIFY_GENERIC,"del",targetkey,c->db->id); 449 } 450 server.dirty++; 451 addReplyLongLong(c,maxlen); /* Return the output string length in bytes. */ 452 } 453 454 /* BITCOUNT key [start end] */ 455 void bitcountCommand(redisClient *c) { 456 robj *o; 457 long start, end, strlen; 458 unsigned char *p; 459 char llbuf[32]; 460 461 /* Lookup, check for type, and return 0 for non existing keys. */ 462 if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL || 463 checkType(c,o,REDIS_STRING)) return; 464 465 /* Set the 'p' pointer to the string, that can be just a stack allocated 466 * array if our string was integer encoded. */ 467 if (o->encoding == REDIS_ENCODING_INT) { 468 p = (unsigned char*) llbuf; 469 strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr); 470 } else { 471 p = (unsigned char*) o->ptr; 472 strlen = sdslen(o->ptr); 473 } 474 475 /* Parse start/end range if any. */ 476 if (c->argc == 4) { 477 if (getLongFromObjectOrReply(c,c->argv[2],&start,NULL) != REDIS_OK) 478 return; 479 if (getLongFromObjectOrReply(c,c->argv[3],&end,NULL) != REDIS_OK) 480 return; 481 /* Convert negative indexes */ 482 if (start < 0) start = strlen+start; 483 if (end < 0) end = strlen+end; 484 if (start < 0) start = 0; 485 if (end < 0) end = 0; 486 if (end >= strlen) end = strlen-1; 487 } else if (c->argc == 2) { 488 /* The whole string. */ 489 start = 0; 490 end = strlen-1; 491 } else { 492 /* Syntax error. */ 493 addReply(c,shared.syntaxerr); 494 return; 495 } 496 497 /* Precondition: end >= 0 && end < strlen, so the only condition where 498 * zero can be returned is: start > end. */ 499 if (start > end) { 500 addReply(c,shared.czero); 501 } else { 502 long bytes = end-start+1; 503 504 addReplyLongLong(c,redisPopcount(p+start,bytes)); 505 } 506 } 507 508 /* BITPOS key bit [start [end]] */ 509 void bitposCommand(redisClient *c) { 510 robj *o; 511 long bit, start, end, strlen; 512 unsigned char *p; 513 char llbuf[32]; 514 int end_given = 0; 515 516 /* Parse the bit argument to understand what we are looking for, set 517 * or clear bits. */ 518 if (getLongFromObjectOrReply(c,c->argv[2],&bit,NULL) != REDIS_OK) 519 return; 520 if (bit != 0 && bit != 1) { 521 addReplyError(c, "The bit argument must be 1 or 0."); 522 return; 523 } 524 525 /* If the key does not exist, from our point of view it is an infinite 526 * array of 0 bits. If the user is looking for the fist clear bit return 0, 527 * If the user is looking for the first set bit, return -1. */ 528 if ((o = lookupKeyRead(c->db,c->argv[1])) == NULL) { 529 addReplyLongLong(c, bit ? -1 : 0); 530 return; 531 } 532 if (checkType(c,o,REDIS_STRING)) return; 533 534 /* Set the 'p' pointer to the string, that can be just a stack allocated 535 * array if our string was integer encoded. */ 536 if (o->encoding == REDIS_ENCODING_INT) { 537 p = (unsigned char*) llbuf; 538 strlen = ll2string(llbuf,sizeof(llbuf),(long)o->ptr); 539 } else { 540 p = (unsigned char*) o->ptr; 541 strlen = sdslen(o->ptr); 542 } 543 544 /* Parse start/end range if any. */ 545 if (c->argc == 4 || c->argc == 5) { 546 if (getLongFromObjectOrReply(c,c->argv[3],&start,NULL) != REDIS_OK) 547 return; 548 if (c->argc == 5) { 549 if (getLongFromObjectOrReply(c,c->argv[4],&end,NULL) != REDIS_OK) 550 return; 551 end_given = 1; 552 } else { 553 end = strlen-1; 554 } 555 /* Convert negative indexes */ 556 if (start < 0) start = strlen+start; 557 if (end < 0) end = strlen+end; 558 if (start < 0) start = 0; 559 if (end < 0) end = 0; 560 if (end >= strlen) end = strlen-1; 561 } else if (c->argc == 3) { 562 /* The whole string. */ 563 start = 0; 564 end = strlen-1; 565 } else { 566 /* Syntax error. */ 567 addReply(c,shared.syntaxerr); 568 return; 569 } 570 571 /* For empty ranges (start > end) we return -1 as an empty range does 572 * not contain a 0 nor a 1. */ 573 if (start > end) { 574 addReplyLongLong(c, -1); 575 } else { 576 long bytes = end-start+1; 577 long pos = redisBitpos(p+start,bytes,bit); 578 579 /* If we are looking for clear bits, and the user specified an exact 580 * range with start-end, we can't consider the right of the range as 581 * zero padded (as we do when no explicit end is given). 582 * 583 * So if redisBitpos() returns the first bit outside the range, 584 * we return -1 to the caller, to mean, in the specified range there 585 * is not a single "0" bit. */ 586 if (end_given && bit == 0 && pos == bytes*8) { 587 addReplyLongLong(c,-1); 588 return; 589 } 590 if (pos != -1) pos += start*8; /* Adjust for the bytes we skipped. */ 591 addReplyLongLong(c,pos); 592 } 593 } 594