英文:
Problem in finding duplicates in a string using bitwise operator
问题
I think, I have implemented right logic in C++ to find duplicates in a string using bit manipulation. but I am unable to understand why it's not printing anything in terminal. can anyone explain why this problem is occurring here ?
#include<stdio.h>
int main(){
char A[] = "finiding#include<stdio.h>
int main(){
char A[] = "finiding\0";
long int H=0;
long int x=0;
for(int i=0; A[i]!='\0'; i++){
x=1;
int temp = A[i];
x = x<<(temp-97);
if(x&H > 0){
printf(" %c duplicate found !",A[i]);
}else{
H = x|H;
}
}
return 0;
}
";
long int H=0;
long int x=0;
for(int i=0; A[i]!='#include<stdio.h>
int main(){
char A[] = "finiding\0";
long int H=0;
long int x=0;
for(int i=0; A[i]!='\0'; i++){
x=1;
int temp = A[i];
x = x<<(temp-97);
if(x&H > 0){
printf(" %c duplicate found !",A[i]);
}else{
H = x|H;
}
}
return 0;
}
'; i++){
x=1;
int temp = A[i];
x = x<<(temp-97);
if(x&H > 0){
printf(" %c duplicate found !",A[i]);
}else{
H = x|H;
}
}
return 0;
}
I have checked the for loop is executing 8 times which is desired according to the string "finding" but control is not going into the if else block which is written into the for loop.
I have used "H" as hashing variable where I am setting the bit which is corresponding to the ASCII code of the character coming into the loop each time. and then using if else I am checking whether the bit is set or not by the concept of masking but still it's not working, and I am unable to understand why it is so.
英文:
I think, I have implemented right logic in C++ to find duplicates in a string using bit manipulation. but I am unable to understand why it's not printing anything in terminal. can anyone explain why this problem is occurring here ?
#include<stdio.h>
int main(){
char A[] = "finiding#include<stdio.h>
int main(){
char A[] = "finiding\0";
long int H=0;
long int x=0;
for(int i=0; A[i]!='\0'; i++){
x=1;
int temp = A[i];
x = x<<(temp-97);
if(x&H > 0){
printf(" %c duplicate found !",A[i]);
}else{
H = x|H;
}
}
return 0;
}
";
long int H=0;
long int x=0;
for(int i=0; A[i]!='#include<stdio.h>
int main(){
char A[] = "finiding\0";
long int H=0;
long int x=0;
for(int i=0; A[i]!='\0'; i++){
x=1;
int temp = A[i];
x = x<<(temp-97);
if(x&H > 0){
printf(" %c duplicate found !",A[i]);
}else{
H = x|H;
}
}
return 0;
}
'; i++){
x=1;
int temp = A[i];
x = x<<(temp-97);
if(x&H > 0){
printf(" %c duplicate found !",A[i]);
}else{
H = x|H;
}
}
return 0;
}
I have checked the for loop is executing 8 times which is desired according to the string "finding" but control is not going into the if else block which is written into the for loop.
I have used "H" as hashing variable where I am setting the bit which is corresponding to the ASCII code of the character coming into the loop each time. and then using if else I am checking whether the bit is set or not by the concept of masking but still it's not working, and I am unable to understand why it is so.
答案1
得分: 1
条件x&H > 0
被解释为x & (H > 0)
,因为>
运算符的优先级高于&
运算符。
添加括号来比较AND操作的结果是否大于零,如(x&H) > 0
。
英文:
The condition x&H > 0
is interpreted as x & (H > 0)
because >
operator has higher precedence than &
operator.
C++ Operator Precedence - cppreference.com
Add parenthesis to compare the result of AND operation with zero like (x&H) > 0
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论