You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
when i learn the function dictScan in redis source code 2.78. i found below code: v = (((v | m0) + 1) & ~m0) | (v & m0); my question is: the result of the code seems same with code: v = ((v | m0) + 1) | (v & m0) which has no "&~m0". i run a test about for these two code. and found that the result is same.
my test code is as below.( i wrote the result of two case into two file and compare the file found that the two file is same.) if i have something wrong, please correct me(please provide the test case for the different result of above two code). many thanks.
#define MAXSIZE 1000000
void binaryResize( int m0,int m1, int v,FILE* fp,FILE* fp1)
{
int temp = v;
do
{
fprintf(fp,"v=%d,",v);
v = (((v | m0) +1) &~m0) | (v & m0);
} while (v & (m0 ^ m1));
v = temp;
do
{
fprintf(fp1,"v=%d,",v);
v = (((v | m0) +1)) | (v & m0);
} while (v & (m0 ^ m1));
}
int main()
{
int m0=7,m1=63;
int v;
FILE* fp,*fp1;
char *path ="/mnt/hgfs/Shared/c++/tmplog";
char *path1 ="/mnt/hgfs/Shared/c++/tmplog1";
fp = fopen(path,"a");
fp1 = fopen(path1,"a");
for( v=1; v <MAXSIZE; v++)
{
binaryResize(m0,m1,v,fp,fp1);
}
v=-1;
fprintf(fp1,"v=%d,",v);
fclose(fp);
fclose(fp1);
}
The size mask for each hash table is guaranteed to be 2^k - 1. ◀️ That invariant is necessary for the final result to remain valid when removing ~m0.
Because m0 is always one minus a power of two, m0 always has uniformly set lower bits.
Because m0 has uniformly set lower bits, adding 1 to the union of m0 and v zeros out all existing lower bit 1 positions in (v | m0).
Because the 1 positions get zero'd out, asking for the AND has no effect because you are ANDing: the the positions zero'd out by the + 1 in (v | m0)against the original 1 positions in m0 which turned into uniform 0 values when you did "NOT ALL MY ONES!"
Hello, this was noticed months ago by @soveran, the reason for taking the redundant code is that it helps to explain what is going on. However we could turn this issue into adding a comment there in order to specify that the final mask is redundant but is there just to make sure the reader understands in bolder terms what is going on.
Activity
mattsta commentedon Aug 11, 2014
You are correct!
Why is
~m0unnecessary?2^k - 1.~m0.m0is always one minus a power of two,m0always has uniformly set lower bits.m0has uniformly set lower bits, adding1to the union ofm0andvzeros out all existing lower bit1positions in(v | m0).1positions get zero'd out, asking for theANDhas no effect because you areANDing: the the positions zero'd out by the+ 1in(v | m0)against the original1positions inm0which turned into uniform0values when you did "NOT ALL MY ONES!"0 & 0 = 0. qed.Or, using a concrete example:
It works without
~m0as long asm0takes the form of2^k - 1, which is always does in our use cases.antirez commentedon Aug 15, 2014
Hello, this was noticed months ago by @soveran, the reason for taking the redundant code is that it helps to explain what is going on. However we could turn this issue into adding a comment there in order to specify that the final mask is redundant but is there just to make sure the reader understands in bolder terms what is going on.
Update RedisTimeSeries module to v8.9.80 (#15336)