this is the only video that actually helped me understand bit manipulation, thank you a lot
@aronpop14476 жыл бұрын
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.
@HanifCarroll4 жыл бұрын
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
@griffintoal44107 жыл бұрын
im a bit lost :)
@turn12106 жыл бұрын
Nice
@chhasnainazam79485 жыл бұрын
hahahaahha me too :D
@MAA-op4gw Жыл бұрын
Thank you so much.
@jagannathanrajagopalan29414 жыл бұрын
why not use XOR to clear a bit? any reason? i mean 1
@adibhargava70553 жыл бұрын
But what if the starting bit is a 1?
@ytdlАй бұрын
Because if you try to clear a bit that is 0 / already cleared it will return a 1.
@adibhargava70553 жыл бұрын
You can also say floor function instead of round down to negative infinity @12:05
@ytdl5 күн бұрын
Never thought i'd see ster teaching bit manipulation
@cschipg46882 жыл бұрын
This guy is so chill presenting. how do you get that relaxed mate
@maheshj012 жыл бұрын
My two cents - Think of it like a normal conversation - and keep your focus on the topic
@Paradise-kv7fn5 жыл бұрын
A easier way for the problem of checking a bit would be return (1
@animodium26703 жыл бұрын
This can be simplified like so: return (1
@LostInNeverland1286 жыл бұрын
ice town costs ice clown his town crown
@UdaraAlwis4 жыл бұрын
yessss! this exactly! I believe only a few will get this reference xD
@eirikolsnes7 жыл бұрын
Thank you! Nice to see how giving all students time to respond resulted in valuable input.
@_sayan_roy_8 жыл бұрын
24:12 , More intuitive for me is:- int modBit(x,pos,state){ mask1 = 1
@_sayan_roy_8 жыл бұрын
+l0ad1 Thanks a lot... :) Now,I do.
@arunsiddharth51847 жыл бұрын
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; }
@AviPars2 жыл бұрын
How would I flip all the bytes ?
@ahmedramzy892 Жыл бұрын
great work 🥰
@satadhi7 жыл бұрын
0:18 i mean seriously !
@satadhi7 жыл бұрын
btw cool videos
@sergioropo30196 жыл бұрын
Very informative and well done. Thank you.
@iXNomad2 жыл бұрын
Why do you use -state if you can just write: x = (x & ~(1
@abdullahnoman86187 жыл бұрын
you could use the xor operator in clearing the bit
@aronpop14476 жыл бұрын
Couldnt have you used the ^ operator for clearing the bits? Exact same code except return x ^ mask?
@aaryandhakal60983 жыл бұрын
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
@olivergreen64364 жыл бұрын
Thanks it's very informative
@panmacabre9895 Жыл бұрын
that's freaking amazing
@yamamarques272 жыл бұрын
//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); }
@gauravsoni59746 жыл бұрын
What is the mask in terms of bit manipulation?
@dtm77433 жыл бұрын
predefined set of bits used to pick and choose which specific bits will be modified by subsequent operations
@tushararora3477 жыл бұрын
What's that sound at 22:41?
@sergioropo30196 жыл бұрын
Obviously is cartoon character exclamation.
@II_superluminal_II4 жыл бұрын
now i get brainfuck holy shit, brainfuck is fast as fuck and now I know why.......
@tauseef10s8 жыл бұрын
//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
@oliviazhai18312 жыл бұрын
This is very helpful!!! Thanks
@mr.anonymous60982 жыл бұрын
Great video! Highly recommend it. Wish he was my teacher
@nishajakhar58676 жыл бұрын
You look damn awesome....And even loved your teaching style..
@vaibhavtiwari69924 жыл бұрын
TF LOL
@cristianrestrepolopez9763 жыл бұрын
Thank you so much this video was extremely helpful :)
@pedroalonsoms2 жыл бұрын
such a good video
@cadupoles7 жыл бұрын
Thank you
@ashishpatel07208 жыл бұрын
very good video thank you very much
@TDLRest4 жыл бұрын
thanks so much for this! you're awesome teacher
@vaibhavtiwari69924 жыл бұрын
THIS IS GOOD FR SOMEONE WHO ALREADY KNOWS THIS STUFF.