C program question: If i have a program that asks the user for a positive integer an i save that in the variable n. And then i have a while statement like so : while (n) { invert (P) n = n & n-1 } what is that while loop saying? Is it saying "while true"? isn't it always true?
Below is a list of `n = n & n-1` output. There are cases where `n==0` and thus the loop will terminate. I'm not sure what it is doing. ``` n: n & n-1 20: 16 19: 18 18: 16 17: 16 16: 0 15: 14 14: 12 13: 12 12: 8 11: 10 10: 8 9: 8 8: 0 7: 6 6: 4 5: 4 4: 0 3: 2 2: 0 1: 0 0: 0 ```
Here is how the loop would iterate out for various `n` It gives `n`, the number iterations, and the values of `n` for the iterations. ``` 0: 1 (0) 1: 1 (0) 2: 1 (0) 3: 2 (2, 0) 4: 1 (0) 5: 2 (4, 0) 6: 2 (4, 0) 7: 3 (6, 4, 0) 8: 1 (0) 9: 2 (8, 0) 10: 2 (8, 0) 11: 3 (10, 8, 0) 12: 2 (8, 0) 13: 3 (12, 8, 0) 14: 3 (12, 8, 0) 15: 4 (14, 12, 8, 0) 16: 1 (0) 17: 2 (16, 0) 18: 2 (16, 0) 19: 3 (18, 16, 0) 20: 2 (16, 0) 21: 3 (20, 16, 0) 22: 3 (20, 16, 0) 23: 4 (22, 20, 16, 0) 24: 2 (16, 0) 25: 3 (24, 16, 0) 26: 3 (24, 16, 0) 27: 4 (26, 24, 16, 0) 28: 3 (24, 16, 0) 29: 4 (28, 24, 16, 0) 30: 4 (28, 24, 16, 0) 31: 5 (30, 28, 24, 16, 0) 32: 1 (0) 33: 2 (32, 0) 34: 2 (32, 0) 35: 3 (34, 32, 0) 36: 2 (32, 0) 37: 3 (36, 32, 0) 38: 3 (36, 32, 0) 39: 4 (38, 36, 32, 0) 40: 2 (32, 0) 41: 3 (40, 32, 0) 42: 3 (40, 32, 0) 43: 4 (42, 40, 32, 0) 44: 3 (40, 32, 0) 45: 4 (44, 40, 32, 0) 46: 4 (44, 40, 32, 0) 47: 5 (46, 44, 40, 32, 0) 48: 2 (32, 0) 49: 3 (48, 32, 0) 50: 3 (48, 32, 0) 51: 4 (50, 48, 32, 0) 52: 3 (48, 32, 0) 53: 4 (52, 48, 32, 0) 54: 4 (52, 48, 32, 0) 55: 5 (54, 52, 48, 32, 0) 56: 3 (48, 32, 0) 57: 4 (56, 48, 32, 0) 58: 4 (56, 48, 32, 0) 59: 5 (58, 56, 48, 32, 0) 60: 4 (56, 48, 32, 0) 61: 5 (60, 56, 48, 32, 0) 62: 5 (60, 56, 48, 32, 0) 63: 6 (62, 60, 56, 48, 32, 0) 64: 1 (0) 65: 2 (64, 0) 66: 2 (64, 0) 67: 3 (66, 64, 0) 68: 2 (64, 0) 69: 3 (68, 64, 0) 70: 3 (68, 64, 0) 71: 4 (70, 68, 64, 0) 72: 2 (64, 0) 73: 3 (72, 64, 0) 74: 3 (72, 64, 0) 75: 4 (74, 72, 64, 0) 76: 3 (72, 64, 0) 77: 4 (76, 72, 64, 0) 78: 4 (76, 72, 64, 0) 79: 5 (78, 76, 72, 64, 0) 80: 2 (64, 0) 81: 3 (80, 64, 0) 82: 3 (80, 64, 0) 83: 4 (82, 80, 64, 0) 84: 3 (80, 64, 0) 85: 4 (84, 80, 64, 0) 86: 4 (84, 80, 64, 0) 87: 5 (86, 84, 80, 64, 0) 88: 3 (80, 64, 0) 89: 4 (88, 80, 64, 0) 90: 4 (88, 80, 64, 0) 91: 5 (90, 88, 80, 64, 0) 92: 4 (88, 80, 64, 0) 93: 5 (92, 88, 80, 64, 0) 94: 5 (92, 88, 80, 64, 0) 95: 6 (94, 92, 88, 80, 64, 0) 96: 2 (64, 0) 97: 3 (96, 64, 0) 98: 3 (96, 64, 0) 99: 4 (98, 96, 64, 0) 100: 3 (96, 64, 0) ```
------------------------------------------------------------- "what is that while loop saying? Is it saying "while true"? isn't it always true?" ------------------------------------------------------------- That means while (n !=0) or while( !(n==0)) n & (n-1) is equals to “0” if "n" have the form: \[2^k\] where k is positive integer. if we start for instance with n=63; the loop ends when its value become equals to 32 because there is no value between 63 and 32 that can be written as \[2^k\]. N.B: the instruction invert (P) in your code is not taken under consideration in this comment.
Join our real-time social learning platform and learn together with your friends!