Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 67hz/1943249d65c7dbaddf2ee4b195e81e74 to your computer and use it in GitHub Desktop.
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.
// 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