bool isSubsequence(char* s, char* t) {
int len = 0;
bool res = false;
int k = 0;
len = strlen(t);
char str[len+1];
uint64_t z;
for (uint64_t i = 0; i < pow(2, len); i++)
{
k = 0;
for (uint64_t j = 0; j < len; j++)
{
z = i & (1 << j);
printf("z value %u",z);
if (z != 0)
{
str[k++] = t[j];
}
}
str[k] = '\0';
if(strcmp(s,str)==0)
{
res= true;
break;
}
printf("%s %d\n",str,i);
str[0] = '\0';
}
return res;
}
Why do I get the error
runtime error: left shift of 1 by 31 places cannot be represented in type 'int' [solution.c]
for the line z = i & (1 << j);
I also tried z= i & (uint64_t(1) <<j) or other options like uint32_t replacing uint64_t also tried only using int for variables z, i and j. It is the leetcode problem, to find subsequence.
note : i have used pow to check all the subsequence of the string. using bit manipulation. yes we can solve the problem using string comparison that is more easier, I just wanted to know here, if the bits get larger than 31, than how to handle it, and what we do to eliminate the error i am getting.
Thank You so much everyone for taking time and answering my question.
z = i & (1 << j);the1is an int.(uint64_t(1) <<j)- this is a bad syntax. If you did(uint64_t)1 <<jit could work. Alternatively1ull << j.powfor integer powers. For integer powers of 2, you could just bitshift instead:1ull << len.%uand%dare also the wrong conversion specifiers foruint64_t. Use%" PRIu64frominttypes.h.main()function.