-
-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added blogs #1901
added blogs #1901
Conversation
Thank you for submitting your pull request! 🙌 We'll review it as soon as possible.). If there are any specific instructions or feedback regarding your PR, we'll provide them here. Thanks again for your contribution! 😊 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update your code based on suggestions
2. **OR (|)**: Sets each bit to 1 if one of the bits is 1. | ||
3. **XOR (^)**: Sets each bit to 1 if only one of the bits is 1. | ||
4. **NOT (~)**: Inverts all bits. | ||
5. **Shift Left (<<)**: Shifts bits to the left, filling with 0s. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace <<
with `<<`
or <<
3. **XOR (^)**: Sets each bit to 1 if only one of the bits is 1. | ||
4. **NOT (~)**: Inverts all bits. | ||
5. **Shift Left (<<)**: Shifts bits to the left, filling with 0s. | ||
6. **Shift Right (>>)**: Shifts bits to the right, filling with the sign bit. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
replace >>
with `>>`
or >>
|
||
def is_odd(n): | ||
return (n & 1) == 1 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write within ```
|
||
def set_bit(n, i): | ||
return n | (1 << i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write within ```
|
||
def clear_bit(n, i): | ||
return n & ~(1 << i) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write within ```
def count_set_bits(n): | ||
count = 0 | ||
while n: | ||
n &= (n - 1) # Clear the least significant bit set | ||
count += 1 | ||
return count | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write within ```
print(count_set_bits(13)) # Output: 3 (binary: 1101) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write within ```
public class BitManipulation { | ||
// Count set bits in an integer | ||
public static int countSetBits(int n) { | ||
int count = 0; | ||
while (n > 0) { | ||
n &= (n - 1); // Clear the least significant bit set | ||
count++; | ||
} | ||
return count; | ||
} | ||
|
||
public static void main(String[] args) { | ||
System.out.println(countSetBits(13)); // Output: 3 (binary: 1101) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
write within ```
📥 Pull Request
Description
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
Fixes # (1748)
Type of change
Checklist: