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

Поделиться
HTML-код
  • Опубликовано: 26 янв 2025

Комментарии • 190

  • @modiji8706
    @modiji8706 10 месяцев назад +243

    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 9 месяцев назад +27

      thanku modi ji ! abki baar 400 paar 👍

    • @modiji8706
      @modiji8706 9 месяцев назад +15

      @@NAMAN-wj7dj Vote dena mt bhulna Abki baar Modi Sarkar

    • @02deepak
      @02deepak 9 месяцев назад +9

      @@modiji8706 modi ji wo recession ka kuch hojata toh badhiya maan lgta voting m

    • @himanshutiwari6614
      @himanshutiwari6614 9 месяцев назад +5

      ​@@02deepakmodiji khud pdh rhe hai dsa😅

    • @modiji8706
      @modiji8706 9 месяцев назад +8

      RastraPati Bhawan ma A jao kl ispe wartalap krte hai

  • @Noob_Coder1234
    @Noob_Coder1234 10 месяцев назад +155

    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 месяца назад +1

      @@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 месяцев назад +41

    Never seen better teacher than u

  • @roshankumar280
    @roshankumar280 7 месяцев назад +41

    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 ❤❤❤

  • @SuvradipDasPhotographyOfficial
    @SuvradipDasPhotographyOfficial 7 месяцев назад +17

    Awesome Striver, done with sliding window and two pointers and now started with bit manipulation yesterday

  • @uzzal.cse42
    @uzzal.cse42 4 месяца назад +3

    Crystal clear explanation. Highly recommended for those who does not know anything about bit manipulation.

  • @nguyengiahuy6292
    @nguyengiahuy6292 10 месяцев назад +15

    struggling so much with this topic alone. Thank you for the series!!!!

  • @AdityaSharma-er3gs
    @AdityaSharma-er3gs 7 месяцев назад +12

    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 ;)

  • @SumitKumar-qg4ps
    @SumitKumar-qg4ps 10 месяцев назад +9

    to clear ith bit(0 indexed), we can just do
    N = n xor (1

    • @chickukoshti3741
      @chickukoshti3741 10 месяцев назад +1

      toggle

    • @AnushkaMishra8
      @AnushkaMishra8 10 месяцев назад +3

      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

  • @tgayush1424
    @tgayush1424 7 месяцев назад +6

    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 месяцев назад +4

    Best Bit Manipulation tutorial ever!!!

  • @knowthrvdo
    @knowthrvdo 10 месяцев назад +4

    Thanks for starting uploading again it is very helpful for us.

  • @ankushrai3155
    @ankushrai3155 11 дней назад

    the efforts this guy puts is just amazing

  • @bhushandharne8827
    @bhushandharne8827 10 месяцев назад +4

    Sir, Your Techniques are superb

  • @KaushalDhruw
    @KaushalDhruw 9 месяцев назад +3

    before this I thought I knew bitwise operations. But the tricks you've shown are awesome. Thanks again.

  • @Karanssharma03
    @Karanssharma03 8 месяцев назад +4

    i dont know how to say thank you but you saved me striver . thank you>3000

  • @coderspathway
    @coderspathway 8 месяцев назад +16

    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 месяцев назад +5

      same here i paid for my gfg self paced but i'm now watching strivers youtube to solve DSA

  • @udayshankar-e6v
    @udayshankar-e6v 10 месяцев назад +5

    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

  • @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

  • @momentcoder
    @momentcoder 17 дней назад

    The best teacher for dsa 🔥🔥

  • @SaswataMaitra
    @SaswataMaitra 3 месяца назад

    17:20 another approach (n&(INT_MAX^(1

  • @tamannaverma4178
    @tamannaverma4178 10 месяцев назад +8

    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 ..

  • @rishavjain5087
    @rishavjain5087 10 дней назад

    This is just too good to be true bro..
    Thanks a lot❤

  • @harshverma9675
    @harshverma9675 10 часов назад

    Bhai aapka knowledge toh kamal ka hai bhai.

  • @KapilSharma56419
    @KapilSharma56419 7 месяцев назад +4

    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 .

  • @kbsce
    @kbsce 8 месяцев назад +1

    Neatly, clearly explained which anyone can easily understand 😊really appreciated your efforts😊

  • @stith_pragya
    @stith_pragya 9 месяцев назад +3

    Understood....Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @SamirKumarRakshit-c4z
    @SamirKumarRakshit-c4z 5 месяцев назад +1

    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)😊

  • @tapasyadimree9651
    @tapasyadimree9651 8 месяцев назад +9

    17:20 when finding ~(1

    • @GoluKumar-sb2si
      @GoluKumar-sb2si 8 месяцев назад +1

      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....

  • @TejeshReddyThippakkagari
    @TejeshReddyThippakkagari 10 месяцев назад +1

    Thanks for teaching every possibility answer for the problems❤❤❤

  • @rithish993
    @rithish993 6 месяцев назад +1

    17:30 instead we can left shift 1 and use xor operators

  • @leaguesonu9354
    @leaguesonu9354 10 месяцев назад +1

    hats off man for your hardwork

  • @rahuljain224
    @rahuljain224 9 месяцев назад +1

    Salute to your hardwork and explanation

  • @nashalafroz
    @nashalafroz 10 месяцев назад +4

    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.

  • @JIHYELEE-h2m
    @JIHYELEE-h2m 6 месяцев назад

    Wow this is essence of bit manipulation!!!!!!!!!! Thank you so much

  • @veerverma5586
    @veerverma5586 10 месяцев назад +3

    Keep going 🏆

  • @ishwarreddy8820
    @ishwarreddy8820 10 месяцев назад

    If striver is making then it will be the best playlist ;

  • @ryugagaming195
    @ryugagaming195 10 месяцев назад +2

    Sir plz make more videos on sliding window

  • @lakshmanlk977
    @lakshmanlk977 9 месяцев назад +1

    Ohh striver such an amazing lecture ..

  • @andrewcenteno3462
    @andrewcenteno3462 6 месяцев назад

    This course was insanely good

  • @akashddeepchitransh4537
    @akashddeepchitransh4537 2 месяца назад

    Knowledge Packed Lecture.

  • @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

  • @rahulpatil2472
    @rahulpatil2472 Месяц назад

    for each problem if you write code that will better, but you already created playlist

  • @aryansinha1818
    @aryansinha1818 2 месяца назад

    38:00 around, using the last method

  • @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

  • @cleweric
    @cleweric 5 месяцев назад

    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

  • @utkarshtiwari7500
    @utkarshtiwari7500 6 месяцев назад +3

    To clear ith bit we can do :-
    return ( n ^ (1

    • @shivanshpatel1898
      @shivanshpatel1898 6 месяцев назад +4

      no we cant because 0 ^ 1 is 1 so if ith bit is already zero it will change it to 1

    • @JunaidhFardeen
      @JunaidhFardeen 3 месяца назад +2

      It is for toggle

  • @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 ...

  • @Benstokes555
    @Benstokes555 10 месяцев назад

    MY MAN IS BACKKKKKKKKKK

  • @DeepakKumar-nq4td
    @DeepakKumar-nq4td 4 месяца назад

    Hila diye bhaiya ji 🎉🎉🎉❤❤

  • @karthik-varma-1579
    @karthik-varma-1579 3 месяца назад +1

    Set> Or
    Clear/Remove -> And
    Toggle -> Xor

  • @mordernmonk194
    @mordernmonk194 3 месяца назад +1

    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

  • @PavanKumar-jz5ow
    @PavanKumar-jz5ow 3 месяца назад

    UNDERSTOOD 🎉🎉😁😁😁😁

  • @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

  • @padduchennamsetti6516
    @padduchennamsetti6516 4 месяца назад +1

    understood ,thanks alot

  • @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 ?

  • @sakshikatiyar5353
    @sakshikatiyar5353 10 месяцев назад

    u are the best teacher

  • @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

  • @anshrathore2731
    @anshrathore2731 10 месяцев назад +1

    Thank you so much BHAIYA 🙏🙏🙏

  • @ShravanKumar-wg9pv
    @ShravanKumar-wg9pv 2 месяца назад

    W lecture. Thank you very much sir.

  • @Mohd-12335
    @Mohd-12335 8 дней назад

    we can swap without using xor ?
    a = a + b;
    b = a - b;
    a = a - b;

  • @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?

  • @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

  • @kingraj4797
    @kingraj4797 9 месяцев назад +1

    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");
    }
    }
    }

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 3 месяца назад

    NICE SUPER EXCELLENT MOTIVATED

  • @Sonu.Singh.28
    @Sonu.Singh.28 3 месяца назад +2

    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

  • @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?

  • @anantmishra5174
    @anantmishra5174 День назад

    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.

  • @Josuke217
    @Josuke217 5 месяцев назад

    Great lecture.

  • @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)

  • @SHINGAVIKAJAL
    @SHINGAVIKAJAL 9 месяцев назад +2

    super amazing!!

  • @UECAshutoshKumar
    @UECAshutoshKumar 10 месяцев назад +1

    Thank you sir 🙏

  • @LemesaElias
    @LemesaElias 4 месяца назад

    In python you can swap without using a 3rd variable like a , b = b , a

    • @Phantomex984
      @Phantomex984 4 месяца назад +2

      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,

  • @riachoudhary__
    @riachoudhary__ 10 месяцев назад

    Thank you for making this video

  • @mohammadanas7620
    @mohammadanas7620 Месяц назад

    22:07 continue

  • @banothutharun2743
    @banothutharun2743 9 месяцев назад

    simple superb sir

  • @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.

  • @CodewithAnuragBassu
    @CodewithAnuragBassu 10 месяцев назад

    Thanku so much bhaiya❤❤🙏🙏🙏🙏

  • @Satyaki-v9n
    @Satyaki-v9n 10 месяцев назад +3

    when we need to count the total set bits from 1 to N , the following code gives TLE when N=10^9 because of the for loop. how can this be optimised ?
    int count(int n)
    {
    int cnt=0;
    while(n!=0)
    {
    if(n&1==1) cnt++;
    n=n>>1;
    }
    // if(n==1) cnt++;
    return cnt;
    }
    int countSetBits(int n)
    {
    //Write your code here
    int ans=0;
    if(n==1) return 1;
    for(int i=1;i

  • @thaman701
    @thaman701 10 месяцев назад

    Great sir.❤

  • @AmandeepSingh-rd6ql
    @AmandeepSingh-rd6ql 9 месяцев назад

    Any reason for using left or right shift operator what is the intuition behind it anyone

  • @MuraliVardhan-s4s
    @MuraliVardhan-s4s Месяц назад

    thank you anna🧡

  • @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

  • @akshaydeogaonkar7120
    @akshaydeogaonkar7120 6 месяцев назад

    How does JavaScript store numbers then? 😃

  • @DeepakPatel-d5v
    @DeepakPatel-d5v 8 месяцев назад +1

    Thanks a lot Bhaiya

  • @vijeshsshetty
    @vijeshsshetty 10 месяцев назад +1

    thank you sir ji

  • @rahulnagwanshi2348
    @rahulnagwanshi2348 8 месяцев назад

    check if a number is a power of two -> !(N & 1)

  • @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 месяца назад +3

      use an example where the ith bit is already 0. You will get to know

  • @saqibaqeel9196
    @saqibaqeel9196 10 месяцев назад

    Is this question available in leet code

  • @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 месяцев назад +2

      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.

  • @sachinvarma9949
    @sachinvarma9949 8 месяцев назад

    Bro loves 13 anyways best lecture on yt.

  • @music-loverFam
    @music-loverFam 5 месяцев назад

    Understood😊

  • @gayathripamarthi8451
    @gayathripamarthi8451 4 месяца назад

    That end music !!!

  • @chirag71269
    @chirag71269 2 месяца назад

    STRIVER 🤩

  • @raveenakumari6724
    @raveenakumari6724 8 месяцев назад

    0 xor 0 is 1 bhaiya.. if we use xor in toggle then it might give us the wrong answer

    • @nova9157
      @nova9157 8 месяцев назад +1

      0 xor 0 is not 1

    • @nova9157
      @nova9157 8 месяцев назад +1

      its 0

  • @DeadPoolx1712
    @DeadPoolx1712 4 месяца назад

    UNDERSTOOD;

  • @RaviKumar-sn6tu
    @RaviKumar-sn6tu 8 месяцев назад

    bhaiya please string start kro !!!!!!!!!!!!!!!!!!!!!

  • @jarvis3551
    @jarvis3551 10 месяцев назад

    can anyone please add timestamps for all questions solved?

  • @shashankgsharma0901
    @shashankgsharma0901 6 месяцев назад

    Understood!