Spoiler alert! Exercise question at the end: The idea is to XOR the two numbers and then count how many 1's does the XOR'ed number has. This works because XOR gives 1 only if one bit is 0 and one is 1, so basically XOR solves our problem. We are faced with the task to count how many 1's does a number has. For that you initialize a counter variable to 0 which will hold the solution, then you add to this variable XOR'ed number & 1 then right shift by 1. You do this while your number is greater then 0.
Since others have posted solutions to the last exercise, I'll post my alternative solution: def compare_bits(n1, n2): count = 0 while n1 or n2: if n1 & 1 != n2 & 1: count += 1 n1, n2 = n1 >> 1, n2 >> 1 return count
Might be a little late or u might have even figured ti out but i did use an XOR and the problem is clear_bit(5,1) presents as a counter example which is why XOR is just for flipping as he showed later but seeing as how u solved his last problem u might have watched the entire video and then found that why XOR doesnt work on ure own but still just wanted to help :D
//count number of different bits #include using namespace std; int main(){ // your code goes here int number1, number2, result, count=0; cin >> number1; cin >> number2; result = number1 ^ number2; while (result){ count += result & 1; result = result >> 1; } cout
//Write a function to count the numnber of bits that are different between two numbers // Time complexity(1); Space complexity(1) public static void numberOfBitsDifferent(int value1, int value2){ int combination = (value1 ^ value2); int count = 0; for (int i=0; i < 32; i++){ int mask = 1 0)? 1 : 0; } System.out.println("Number of different bits: "+ count); }
Spoiler alert! Exercise question at the end:
The idea is to XOR the two numbers and then count how many 1's does the XOR'ed number has. This works because XOR gives 1 only if one bit is 0 and one is 1, so basically XOR solves our problem. We are faced with the task to count how many 1's does a number has. For that you initialize a counter variable to 0 which will hold the solution, then you add to this variable XOR'ed number & 1 then right shift by 1. You do this while your number is greater then 0.
this is the only video that actually helped me understand bit manipulation, thank you a lot
Thank you!
Nice to see how giving all students time to respond resulted in valuable input.
Very informative and well done. Thank you.
im a bit lost :)
Nice
hahahaahha me too :D
Thank you so much this video was extremely helpful :)
Since others have posted solutions to the last exercise, I'll post my alternative solution:
def compare_bits(n1, n2):
count = 0
while n1 or n2:
if n1 & 1 != n2 & 1:
count += 1
n1, n2 = n1 >> 1, n2 >> 1
return count
Thanks it's very informative
ice town costs ice clown his town crown
yessss! this exactly! I believe only a few will get this reference xD
You can also say floor function instead of round down to negative infinity @12:05
Here you go:
int result(int num,int num2){
int x =num ^ num2;
int count=0;
while(x){
x=(x)&(x-1);
count++;
}
return count;
}
How would I flip all the bytes ?
that's freaking amazing
Great video! Highly recommend it. Wish he was my teacher
Thank you so much.
This is very helpful!!! Thanks
great work 🥰
Couldnt have you used the ^ operator for clearing the bits? Exact same code except return x ^ mask?
Might be a little late or u might have even figured ti out but i did use an XOR and the problem is clear_bit(5,1) presents as a counter example which is why XOR is just for flipping as he showed later but seeing as how u solved his last problem u might have watched the entire video and then found that why XOR doesnt work on ure own but still just wanted to help :D
A easier way for the problem of checking a bit would be
return (1
This can be simplified like so:
return (1
This guy is so chill presenting. how do you get that relaxed mate
My two cents
- Think of it like a normal conversation
- and keep your focus on the topic
very good video
thank you very much
thanks so much for this! you're awesome teacher
24:12 , More intuitive for me is:-
int modBit(x,pos,state){
mask1 = 1
+l0ad1 Thanks a lot... :) Now,I do.
you could use the xor operator in clearing the bit
why not use XOR to clear a bit? any reason? i mean 1
But what if the starting bit is a 1?
What is the mask in terms of bit manipulation?
predefined set of bits used to pick and choose which specific bits will be modified by subsequent operations
Thank you
such a good video
Why do you use -state if you can just write:
x = (x & ~(1
//count number of different bits
#include
using namespace std;
int main(){
// your code goes here
int number1, number2, result, count=0;
cin >> number1;
cin >> number2;
result = number1 ^ number2;
while (result){
count += result & 1;
result = result >> 1;
}
cout
You look damn awesome....And even loved your teaching style..
TF LOL
What's that sound at 22:41?
Obviously is cartoon character exclamation.
0:18 i mean seriously !
btw cool videos
//Write a function to count the numnber of bits that are different between two numbers
// Time complexity(1); Space complexity(1)
public static void numberOfBitsDifferent(int value1, int value2){
int combination = (value1 ^ value2);
int count = 0;
for (int i=0; i < 32; i++){
int mask = 1 0)? 1 : 0;
}
System.out.println("Number of different bits: "+ count);
}
now i get brainfuck holy shit, brainfuck is fast as fuck and now I know why.......
THIS IS GOOD FR SOMEONE WHO ALREADY KNOWS THIS STUFF.
i love you
This guys pretty cute
I take it back hes very cute