SETBIT

Redis Developer Course Redis Cloud Service Redis-Gate Server

Sets or clears the bit at offset in the string value at a key

Usage is SETBIT key offset value.   Value can only be 1 and 0. Set is 1 and clear is 0.   If the key does not exist, a new string value is created. The offset is greater than or equal to 0, and smaller than 232 - 1 (this limits bitmaps to 512MB).   When the string at key is grown, added bits are set to 0.
This SETBIT command is one of five bit management commands.
If the management data in bit units can save memory.   For example, to manage 100 attendees can save a lot of memory(RAM) compared to other data types.

Return value

Integer reply: 1 or 0, the original bit value stored at offset.

Example

command>set key A
result>OK
command>setbit key 6 1
result>0
command>get key
result>C

Animation


메모리 사용량 테스트

메모리 사용량 테스트

MegabytesBytesPower of 2BitsPower of 2
2MB2,097,151221 -116,777,215224 -1
4MB4,194,303222 -133,554,431225 -1
8MB8,388,607223 -167,108,863226 -1
16MB16,777,215224 -1134,217,727227 -1
32MB33,554,431225 -1268,435,455228 -1
64MB67,108,863226 -1536,870,911229 -1
128MB134,217,727227 -11,073,741,823230 -1
256MB268,435,455228 -12,147,483,647231 -1
512MB536,870,911229 -14,294,967,295232 -1

Redis Server 3.2.2 and CentOS 7

CommandsIncrease size(MB)Bytes
setbit key2 16777215 12.5MB2,621,520
setbit key4 33554431 15MB5,242,960
setbit key8 67108863 110MB10,485,840
setbit key16 134217727 120MB20,971,816
setbit key32 268435455 140MB41,943,552
setbit key64 536870911 180MB83,907,672
setbit key124 1073741823 1160MB167,793,968
setbit key256 2147483647 1320MB335,566,344
setbit key512 4294967295 1640MB671,110,880

비트 4,294,967,296(232)을 입력하면 다음과 같은 에러가 발생한다.   그래서 2의 거듭제곱 비트에 -1을 한 것이다.

127.0.0.1:7000> setbit key512 4294967296 1
(error) ERR bit offset is not an integer or out of range


Syntax

SETBIT key offset value

  • Available since 2.2.0.
  • Time complexity(Logical processing time): O(1)
  • This document is based on Redis server version 3.2.2.
Related Commands GETBIT, BITOP, BITCOUNT, BITPOS
Clients for Java Jedis, Lettuce, Redisson
Clients for C Hiredis

To practice the command, click here to run Redis Web Client


<< GETSET SETBIT GETBIT >>

Click for a question or to see comments.  Comments :    Viewed :

Email 답글이 올라오면 이메일로 알려드리겠습니다.