fork download
  1. //
  2. // main.cpp
  3. // Bitmask
  4. //
  5. // Created by Himanshu on 18/07/22.
  6. //
  7.  
  8. #include<iostream>
  9. #include<cmath>
  10. #include <bitset>
  11. #include<vector>
  12. #define N 3
  13. using namespace std;
  14.  
  15. void findSubsets (int k, int ki[]) {
  16.  
  17. long subsetCount = (long int)pow(2,(double)N);
  18.  
  19. for (long i=0; i<subsetCount; i++) {
  20.  
  21. int pos = 0; //variable for current bit position (0th - N-1th)
  22.  
  23. long x = i; //integer to be used for bitmasking (0 <= x < 2^N)
  24.  
  25. bool flag = false;
  26.  
  27. bitset<N> subset;
  28.  
  29. while (x > 0) {
  30.  
  31. //Checking if the last bit from the right is set or not
  32. if (x&1) {
  33.  
  34. //Checking if the current bit is the excluded (ki) bit or not
  35. for (int j=0; j<k; j++) {
  36. if (pos == ki[j]) {
  37. flag = true;
  38. }
  39. }
  40.  
  41. if (flag) {
  42. break;
  43. } else {
  44. //Adding current bit to the subset
  45. subset.set(pos, 1);
  46. }
  47.  
  48. } else {
  49. //Setting the current bit as 0 if it's unset in x
  50. subset.set(pos, 0);
  51. }
  52.  
  53. //shifting 1 bit to right, to check the next bit
  54. //and incresing position (pos) of the current bit by 1
  55. x = x >> 1;
  56. pos++;
  57. }
  58.  
  59. if (!flag) {
  60. cout<<subset<<endl;
  61. }
  62. }
  63.  
  64. }
  65.  
  66. int main() {
  67.  
  68. int k = 1;
  69. int *ki = new int[k]();
  70. ki[0] = 0;
  71.  
  72. cout<<"Subsets with 0th bit unset:"<<endl;
  73. findSubsets(k, ki);
  74.  
  75. return 0;
  76. }
Success #stdin #stdout 0.01s 5396KB
stdin
Standard input is empty
stdout
Subsets with 0th bit unset:
000
010
100
110