L2. Must Know Tricks in Bit Manipulation | Swap two numbers without third variable

  Рет қаралды 201,538

take U forward

take U forward

Күн бұрын

Notes/Codes/Problem links under day 8 of A2Z DSA Course: takeuforward.o...
Follow us on our other social media handles: linktr.ee/take...

Пікірлер: 190
@modiji8706
@modiji8706 10 ай бұрын
1. Swap two numbers - 00:00 2. Check if ith bit is set or not - 3:47 3. set ith Bit - 10:47 4. clear ith Bit - 14:27 5. Toggle ith Bit - 17:52 6.Remove the last set Bit - 21:23 7. Check if a number is power of 2 or not - 28:26 8.count the number of set bits - 31:24
@NAMAN-wj7dj
@NAMAN-wj7dj 10 ай бұрын
thanku modi ji ! abki baar 400 paar 👍
@modiji8706
@modiji8706 10 ай бұрын
@@NAMAN-wj7dj Vote dena mt bhulna Abki baar Modi Sarkar
@02deepak
@02deepak 9 ай бұрын
@@modiji8706 modi ji wo recession ka kuch hojata toh badhiya maan lgta voting m
@himanshutiwari6614
@himanshutiwari6614 9 ай бұрын
​@@02deepakmodiji khud pdh rhe hai dsa😅
@modiji8706
@modiji8706 9 ай бұрын
RastraPati Bhawan ma A jao kl ispe wartalap krte hai
@Noob_Coder1234
@Noob_Coder1234 10 ай бұрын
REMEMBER IF STRIVER IS MAKING , THEN IT WILL BE THE BEST PLAYLIST ON BIT MANIPULATION
@siddiqabr7110
@siddiqabr7110 3 ай бұрын
Have you ever watched kunal kushwaha's videos
@aayambhatt5363
@aayambhatt5363 3 ай бұрын
@@siddiqabr7110 his videos arent any good
@mayankagarwal6775
@mayankagarwal6775 2 ай бұрын
@@aayambhatt5363 yes !! I had started my dsa journey from kunal but now shifted to striver after understanding basics of dsa.
@lillyput2275
@lillyput2275 Ай бұрын
@@aayambhatt5363I don’t understand his way of teaching
@CDK-Tech
@CDK-Tech Ай бұрын
@@aayambhatt5363 His videos was once good , now it's not 😭
@fazfavas-d6j
@fazfavas-d6j 10 ай бұрын
Never seen better teacher than u
@Benstokes555
@Benstokes555 10 ай бұрын
YESS
@raorao7329
@raorao7329 6 ай бұрын
true
@roshankumar280
@roshankumar280 7 ай бұрын
One-Liner: 1) Swapping Two Numbers : Num1=(Num1^Num2); Num2=(Num1^Num2); Num1=(Num1^Num2); 2) Check If i’th bit is set or not: if((Num&(1
@Monjaverse
@Monjaverse 4 ай бұрын
Love you bro ❤❤❤
@nguyengiahuy6292
@nguyengiahuy6292 10 ай бұрын
struggling so much with this topic alone. Thank you for the series!!!!
@uzzal.cse42
@uzzal.cse42 4 ай бұрын
Crystal clear explanation. Highly recommended for those who does not know anything about bit manipulation.
@SumitKumar-qg4ps
@SumitKumar-qg4ps 10 ай бұрын
to clear ith bit(0 indexed), we can just do N = n xor (1
@chickukoshti3741
@chickukoshti3741 10 ай бұрын
toggle
@AnushkaMishra8
@AnushkaMishra8 10 ай бұрын
Yes , but it will work same as toggling if the bit is not set and we don't want that , if the bit will be 0 then also it will be changed to 1 if we do this.
@HaamroNotes
@HaamroNotes 8 ай бұрын
@@AnushkaMishra8 yeah bro
@SuvradipDasPhotographyOfficial
@SuvradipDasPhotographyOfficial 7 ай бұрын
Awesome Striver, done with sliding window and two pointers and now started with bit manipulation yesterday
@AdityaSharma-er3gs
@AdityaSharma-er3gs 7 ай бұрын
33:00 here you can simply do this public static int countSetBit(int num){ int count = 0; int one = 1; while(num > 0){ if((num & one) == 1){ count++; } num = num >> 1; } return count; }
@priyanshugupta7840
@priyanshugupta7840 Ай бұрын
it is almost the same thing taking the same time except you create one more variable num ;)
@udayshankar-e6v
@udayshankar-e6v 10 ай бұрын
Many times I thought to comment on his post and lastly just leaving it by pressing like button.. thinking that kya kya bolega log,,! Is there anyone else like me ? Aisa koi h Banda Jo striver se bhi accha padhata ho ! I don't think so some one exist ❤
@pranavmittal9619
@pranavmittal9619 10 ай бұрын
ek h lekin me nahi batunga bas hint de deta hu : 2 crore ka package chod diya bande ne
@udayshankar-e6v
@udayshankar-e6v 10 ай бұрын
​@@pranavmittal9619Bhai hai to bata do jara hum bhi Jane kon h, teacher hi na , terrorist thode h Jo bata na paoge 😂 kahi tum hi to nahi ho wo 2 cr vala Banda Jo chhup chup ke bit manipulation ka maja le rahe ho😊
@raorao7329
@raorao7329 6 ай бұрын
@@pranavmittal9619 who??
@pranavmittal9619
@pranavmittal9619 6 ай бұрын
@@raorao7329 bhai coder army wale rohit negi bhaiya hai hindi lanuage ke liye bas striver best hai lekin Full english me concept grasp karne me dikkat bhi aati hai or striver ka code high level ka rehta hai or rohit negi ka low level ka code lekin output same rehta
@tgayush1424
@tgayush1424 7 ай бұрын
amazing video covering all concepts, soon will be mastering bit manipulations by completing your playlist.Before i used to be scared of seeing bits but now it's easy , even i started to use , today i used in one question and solved that question easily.
@codingp110
@codingp110 6 ай бұрын
Best Bit Manipulation tutorial ever!!!
@knowthrvdo
@knowthrvdo 10 ай бұрын
Thanks for starting uploading again it is very helpful for us.
@bhushandharne8827
@bhushandharne8827 10 ай бұрын
Sir, Your Techniques are superb
@ankushrai3155
@ankushrai3155 13 күн бұрын
the efforts this guy puts is just amazing
@KaushalDhruw
@KaushalDhruw 9 ай бұрын
before this I thought I knew bitwise operations. But the tricks you've shown are awesome. Thanks again.
@tamannaverma4178
@tamannaverma4178 10 ай бұрын
Thanks for all these efforts :)
@ce038_divanshsingh3
@ce038_divanshsingh3 7 ай бұрын
i just stuck to your glow in comment section 😂😂 , forgot about the lecture that is running in the background ..
@Karanssharma03
@Karanssharma03 8 ай бұрын
i dont know how to say thank you but you saved me striver . thank you>3000
@coderspathway
@coderspathway 8 ай бұрын
I have paid DSA course from GFG. But not able to understand BIT manipulation. After watching this playlist by striver I feel the striver did it better than any other paid course in the market.
@Thinker-360
@Thinker-360 7 ай бұрын
same here i paid for my gfg self paced but i'm now watching strivers youtube to solve DSA
@harshverma9675
@harshverma9675 2 күн бұрын
Bhai aapka knowledge toh kamal ka hai bhai.
@kbsce
@kbsce 8 ай бұрын
Neatly, clearly explained which anyone can easily understand 😊really appreciated your efforts😊
@rishavjain5087
@rishavjain5087 12 күн бұрын
This is just too good to be true bro.. Thanks a lot❤
@momentcoder
@momentcoder 19 күн бұрын
The best teacher for dsa 🔥🔥
@stith_pragya
@stith_pragya 9 ай бұрын
Understood....Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@rahuljain224
@rahuljain224 9 ай бұрын
Salute to your hardwork and explanation
@leaguesonu9354
@leaguesonu9354 10 ай бұрын
hats off man for your hardwork
@TejeshReddyThippakkagari
@TejeshReddyThippakkagari 10 ай бұрын
Thanks for teaching every possibility answer for the problems❤❤❤
@SamirKumarRakshit-c4z
@SamirKumarRakshit-c4z 5 ай бұрын
int count = 0; int num = 7; while(num > 0){ num = (num & (num-1)); ++count; } print(count) We can also do this in order to count the set bit (1)😊
@JIHYELEE-h2m
@JIHYELEE-h2m 6 ай бұрын
Wow this is essence of bit manipulation!!!!!!!!!! Thank you so much
@lakshmanlk977
@lakshmanlk977 9 ай бұрын
Ohh striver such an amazing lecture ..
@ishwarreddy8820
@ishwarreddy8820 10 ай бұрын
If striver is making then it will be the best playlist ;
@SaswataMaitra
@SaswataMaitra 3 ай бұрын
17:20 another approach (n&(INT_MAX^(1
@rohan8758
@rohan8758 4 ай бұрын
*java code* Set ith bit from last, conside 0 based indexing (10:47 ): public static void main(String[] args) { int n = 9; int k = 2; int second_desired_num = 1
@veerverma5586
@veerverma5586 10 ай бұрын
Keep going 🏆
@akashddeepchitransh4537
@akashddeepchitransh4537 2 ай бұрын
Knowledge Packed Lecture.
@Benstokes555
@Benstokes555 10 ай бұрын
MY MAN IS BACKKKKKKKKKK
@KapilSharma56419
@KapilSharma56419 7 ай бұрын
there is a question on GFG : Count total set bits You are given a number N. Find the total count of set bits for all numbers from 1 to N(both inclusive). which shows TLE by all your methods can you please explain it .
@andrewcenteno3462
@andrewcenteno3462 6 ай бұрын
This course was insanely good
@itxamanpanwar8556
@itxamanpanwar8556 7 ай бұрын
🙏🙏TQ bhaiya for such a good explanation is topic ma smj hi ni aa raaha tha ab sb easy lg r ha
@sakshikatiyar5353
@sakshikatiyar5353 10 ай бұрын
u are the best teacher
@DeepakKumar-nq4td
@DeepakKumar-nq4td 4 ай бұрын
Hila diye bhaiya ji 🎉🎉🎉❤❤
@tapasyadimree9651
@tapasyadimree9651 9 ай бұрын
17:20 when finding ~(1
@GoluKumar-sb2si
@GoluKumar-sb2si 8 ай бұрын
same doubt bro
@AkOp-bf9vm
@AkOp-bf9vm 8 ай бұрын
i think we cannot access 2's complement part and can only access negated part of the number maybe i am not sure
@CodeByRobin
@CodeByRobin 4 ай бұрын
same doubt bro ,, I raised this on lecture one as well, but bhaiya is not replying....
@padduchennamsetti6516
@padduchennamsetti6516 4 ай бұрын
understood ,thanks alot
@utkarshtiwari7500
@utkarshtiwari7500 6 ай бұрын
To clear ith bit we can do :- return ( n ^ (1
@shivanshpatel1898
@shivanshpatel1898 6 ай бұрын
no we cant because 0 ^ 1 is 1 so if ith bit is already zero it will change it to 1
@JunaidhFardeen
@JunaidhFardeen 3 ай бұрын
It is for toggle
@anshrathore2731
@anshrathore2731 10 ай бұрын
Thank you so much BHAIYA 🙏🙏🙏
@ryugagaming195
@ryugagaming195 10 ай бұрын
Sir plz make more videos on sliding window
@ShravanKumar-wg9pv
@ShravanKumar-wg9pv 2 ай бұрын
W lecture. Thank you very much sir.
@rithish993
@rithish993 6 ай бұрын
17:30 instead we can left shift 1 and use xor operators
@UECAshutoshKumar
@UECAshutoshKumar 10 ай бұрын
Thank you sir 🙏
@nashalafroz
@nashalafroz 10 ай бұрын
17:30 sir if we use not operator wouldnt the number converted into its 2's complement
@janarddansarkar2694
@janarddansarkar2694 10 ай бұрын
same doubt. Someone please clearify
@janarddansarkar2694
@janarddansarkar2694 10 ай бұрын
What I am assuming is since we are not storing the negative answer, so 2's complement is not used. If the answer was negative, then to store it, 2's complement would have been used. This is my assumption though. Not sure
@human0225
@human0225 10 ай бұрын
Any solution to this I am still struggling with this.
@kumarnishantnitallahabad160
@kumarnishantnitallahabad160 9 ай бұрын
same doubt
@CodeByRobin
@CodeByRobin 4 ай бұрын
Same doubt .. You got the answer.
@ganeshjaggineni4097
@ganeshjaggineni4097 3 ай бұрын
NICE SUPER EXCELLENT MOTIVATED
@SHINGAVIKAJAL
@SHINGAVIKAJAL 9 ай бұрын
super amazing!!
@Josuke217
@Josuke217 5 ай бұрын
Great lecture.
@PavanKumar-jz5ow
@PavanKumar-jz5ow 3 ай бұрын
UNDERSTOOD 🎉🎉😁😁😁😁
@riachoudhary__
@riachoudhary__ 10 ай бұрын
Thank you for making this video
@rahulpatil2472
@rahulpatil2472 Ай бұрын
for each problem if you write code that will better, but you already created playlist
@MuraliVardhan-s4s
@MuraliVardhan-s4s Ай бұрын
thank you anna🧡
@CodewithAnuragBassu
@CodewithAnuragBassu 10 ай бұрын
Thanku so much bhaiya❤❤🙏🙏🙏🙏
@DeepakPatel-d5v
@DeepakPatel-d5v 8 ай бұрын
Thanks a lot Bhaiya
@edsongeorgerebello547
@edsongeorgerebello547 Ай бұрын
In the clearing the set bit won't the negation of the left shift 1 make it a 2's complement and the program will add a 1 to it ?
@banothutharun2743
@banothutharun2743 9 ай бұрын
simple superb sir
@vijeshsshetty
@vijeshsshetty 10 ай бұрын
thank you sir ji
@karthik-varma-1579
@karthik-varma-1579 3 ай бұрын
Set> Or Clear/Remove -> And Toggle -> Xor
@aryansinha1818
@aryansinha1818 2 ай бұрын
38:00 around, using the last method
@uditgarg6508
@uditgarg6508 7 ай бұрын
for checking ith bit set just do right shift by ith and check the resukting num is odd. if it is , it is set. else , not ...
@ReNaq313
@ReNaq313 9 ай бұрын
39:00 -> repeatedly removing the rightmost set bits and taking the count of times we did this operation would give us the number of set bits in a number
@CodeByRobin
@CodeByRobin 4 ай бұрын
somewhere in the comments , i read "computer store binary as flipped but while showing us it do 2's complement to show us -ve binary in interger" is this right?
@cleweric
@cleweric 6 ай бұрын
in case of power of 2, n & (n-1) == 0 will not work if n = 0, as 0 is not a power of two, we can rather use n & (-n) == n, which mean AND n with its 2's complement it would result n, if its a power of 2
@thaman701
@thaman701 10 ай бұрын
Great sir.❤
@gayathripamarthi8451
@gayathripamarthi8451 4 ай бұрын
That end music !!!
@Sahilkumar-hj4vm
@Sahilkumar-hj4vm 6 ай бұрын
at 17:30 striver do the NOT operator and it flipped all the bits making it a negative number then according to full NOT operator it do 2's complement also but striver stopped at flipping can someone explain me why? edited - ok i get it why computer store binary as flipped but while showing us it do 2's complement to show us -ve binary in interger
@Sonu.Singh.28
@Sonu.Singh.28 3 ай бұрын
If you find the swapping using xor confusing, then instead of xor operation use the addition and subtraction operation, it will do the job. a = 17 b = 27 a = a + b # a becomes 17 + 27 = 44 b = a - b # b becomes (17 + 27) - 27 = 44 - 27 = 17 a = a - b # a becomes (17 + 27) - 17 = 27
@anantmishra5174
@anantmishra5174 3 күн бұрын
I HAVE ONE QUESTION WHEN WE ARE CHECKING IF I'th BIT IS SET OR NOT WHILE DOING RIGHT SHIFT OF NUMBER (n-1) TIMES. WOULDN'T IT CHANGES THE INPUT NUMBER SUPPLIED.
@Neo-mx2yf
@Neo-mx2yf 7 ай бұрын
So in prev video, a method was taught to find ~x but it is a bit unclear. Let me try clear it up. Actually, ~x is just 1's complement of x, i.e., flip all bits. Eg: ~19 = ~(010011) = (101100) in binary = -20 in decimal Now we know (-x) is actually 2's complement of x. So what he taught is actually to find -x manually. Take prev eg, ~(010011) = (101100) in binary = -(2's complement of 101100) in decimal = -(010100) in decimal = -20 Take other way, ~(-20) = ~(101100)=010011 in binary=(directly) 19 Note: For easiness just assume that instead of 32 bits, there are only 6 bits here.
@mahaboobpeershaik658
@mahaboobpeershaik658 5 ай бұрын
Can we get slides notes, @takeUforward bro?
@music-loverFam
@music-loverFam 5 ай бұрын
Understood😊
@LemesaElias
@LemesaElias 4 ай бұрын
In python you can swap without using a 3rd variable like a , b = b , a
@Phantomex984
@Phantomex984 4 ай бұрын
u can do swap without using 3rd var in all languages just use a = 3, b = 7, b+=a, a = b-a, b = b-a, Output : , a = 7, b = 3,
@k.k.harjeeth5422
@k.k.harjeeth5422 6 ай бұрын
unset the right most set bit : n&(n-1) set the right most unset bit : n | (n+1)
@abhinavraj6507
@abhinavraj6507 4 ай бұрын
At 17:20 striver is not taking 2's complement for NOT operator ? Last video he taught that if after flipping the number becomes negative , we take 2's complement , anyone please help ?
@CodeByRobin
@CodeByRobin 4 ай бұрын
same doubt. Please let me know, if you got the answer
@Mohd-12335
@Mohd-12335 10 күн бұрын
we can swap without using xor ? a = a + b; b = a - b; a = a - b;
@ansariaburehan263
@ansariaburehan263 Ай бұрын
Love it.
@aayushgakhar3525
@aayushgakhar3525 6 ай бұрын
in clearing the ith bit ques shouldnt we take -1 as while finding not of 1 its sign bit also changes in first step and so it becomes -ve and so we have to find its 2s compli
@CodeByRobin
@CodeByRobin 4 ай бұрын
same doubt bro,, if you got the answer. Pease reply
@DeadPoolx1712
@DeadPoolx1712 4 ай бұрын
UNDERSTOOD;
@Learnprogramming-q7f
@Learnprogramming-q7f 9 ай бұрын
Thank you Bhaiya
@akshatchoudhary2783
@akshatchoudhary2783 8 ай бұрын
Bhaiya in the Clear bit Soluton can we use XOR operation? like below n = 13, i =2 13 -> 1101 1
@CodeByRobin
@CodeByRobin 4 ай бұрын
use an example where the ith bit is already 0. You will get to know
@kingraj4797
@kingraj4797 9 ай бұрын
I think right and left shift operation or not required for finding the i th bit is set(1) or reset(0) for given binary digits Here I have code please verify it it is taking O(1) time complexity to find it.. // ith bit is set or reset import java.util.*; class SetOrReset{ public static void main(String args[]) { Scanner sc=new Scanner(System.in); String str=sc.next(); int i=1; int j=str.length()-1; int x=str.charAt(j-i)-'0'; if(x==1){ System.out.println("Set"); }else{ System.out.println("Reset"); } } }
@shashankgsharma0901
@shashankgsharma0901 6 ай бұрын
Understood!
@sachinvarma9949
@sachinvarma9949 8 ай бұрын
Bro loves 13 anyways best lecture on yt.
@amanasrani6405
@amanasrani6405 6 ай бұрын
Amazinngggggggggg❤
@yash30095
@yash30095 17 күн бұрын
understood!
@ifirdaus0_0
@ifirdaus0_0 10 ай бұрын
can i use (n ^ (1
@aayushgupta7839
@aayushgupta7839 10 ай бұрын
yes you can , I have also done the same for the code ninja question and it cleared all the test cases.
@coder_07
@coder_07 10 ай бұрын
Yes , but it will work same as toggling if the bit is not set and we don't want that , if the bit will be 0 then also it will be changed to 1 if we do this.
@chirag71269
@chirag71269 2 ай бұрын
STRIVER 🤩
@ok-jg9jb
@ok-jg9jb 10 ай бұрын
Thanks❤
@swaroopvengali
@swaroopvengali 5 ай бұрын
Python 1 liners- ============= Swapping two numbers: a = a ^ b; b = a ^ b; a = a ^ b Check if i'th bit is set or not: print("SET" if (Num & (1
@mordernmonk194
@mordernmonk194 3 ай бұрын
One-Liner: 1) Swapping Two Numbers : Num1=(Num1^Num2); Num2=(Num1^Num2); Num1=(Num1^Num2); 0000101 = num1 ^0001001 = num2 ________ 0001100 xor of num1 and num2 stored in num1 0001001 = num2 ----- 0000101 xor of (num1 and num2) and num2 stored in num2 0001100 ----- 0001001 2) Check If i’th bit is set or not: if (( Num &(1
@TechieClasher
@TechieClasher 8 ай бұрын
Helpfull
@KartikeyTT
@KartikeyTT 6 ай бұрын
tysm sir
@kalkeshvyas7897
@kalkeshvyas7897 2 ай бұрын
great
@rahulnagwanshi2348
@rahulnagwanshi2348 8 ай бұрын
check if a number is a power of two -> !(N & 1)
@riddhithbanerjee
@riddhithbanerjee 3 ай бұрын
In the power of 2 case...what if the number is 0 or INT_MIN then for 0 it will be n&n-1 = 0&-1 means 0...0000 & 1...1111 its coming 0 then it will return true but actually its not power of 2 And in case of INT_MIN n&n-1 the n-1 value is getting overflowed...please solve the doubt anyone.
L3. Minimum Bit Flips to Convert Number | Bit Manipulation
7:00
take U forward
Рет қаралды 78 М.
What's Your ENGLISH LEVEL? Take This Test!
21:31
Brian Wiles
Рет қаралды 4,2 МЛН
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН
How 2010s YouTube DESTROYED Sonic
18:04
chaomix
Рет қаралды 338 М.
Major Announcement | TUF+ V2 and System Design Playlist 🔥
19:12
take U forward
Рет қаралды 66 М.
Кладовка за 2600$.
22:00
АУКЦИОН КОНТЕЙНЕРОВ В США
Рет қаралды 208 М.
Sigma Kid Mistake #funny #sigma
00:17
CRAZY GREAPA
Рет қаралды 30 МЛН