Created
March 27, 2019 00:18
-
-
Save 67hz/1943249d65c7dbaddf2ee4b195e81e74 to your computer and use it in GitHub Desktop.
A simple program to illustrate bitwise masking and merging to determine if a string contains duplicates.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple program to illustrate bitwise ops to check string for duplicates | |
#include <iostream> | |
using namespace std; | |
int main() | |
{ | |
char * A = new char[20]; | |
cout << "Enter string to be checked for duplicates" << endl; | |
cin >> A; | |
long int H=0, x=0; | |
int i; | |
for(i=0; A[i] != '\0'; i++) | |
{ | |
x = 1; | |
x = x << (A[i] - 97); | |
if((x&H) > 0) // masking - checking if bit is on/off | |
cout << A[i] << " is a duplicate" << endl; | |
else | |
H = x | H; // merging - setting bit | |
} | |
delete [] A; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment