Learn these 10 Bitwise Tricks Or Regret Later | Competitive Programming Tricks Part 2

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

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

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

    This is extremely helpful. One of the best videos on bit manipulation.

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

    I was in problem about the bitwise operator but now I found the solution
    Thankyou very much

  • @kaylee-xie
    @kaylee-xie Год назад +7

    Good video! The lecture is straightforward and easy to understand!

  • @llarn.i5103
    @llarn.i5103 Месяц назад

    literally the best bitwise video, clear and straight to the point, thanks a lot.

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

    That was good info in a short and concise video . Loved it !

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

    This one is fun:
    Find the unique number in an array. For example arr = [2, 4, 3, 5, 1, 4, 2, 1, 5] where 3 is the unique number.
    Solution:
    arr.reduce((a, b) => a^b)
    This works if there are an odd number of occurrences of the unique number and an even number of occurrences of all other numbers. When you XOR all the numbers in the array, the even occurrences of the repeated numbers will cancel each other out (since x ^ x = 0), and you will be left with the unique number (since it only occurs once).

  • @anshulsinghtomar780
    @anshulsinghtomar780 Год назад +5

    I have seen the complete lecture & it was amazing .
    The way of delivering concepts was top Notch , video & audio quality .
    Your content deserves more reach.
    Thanks for the Concepts brother.
    Respect++;

  • @aarushiagarwal8938
    @aarushiagarwal8938 Год назад +12

    It would be really helpful if you would make a video on how to apply binary search technique to solve hard cp problems sometimes it happens that we might not even know that the problem involves binary search.
    Btw this one is again awsome and great video on bits👍

    • @shri9229
      @shri9229 Год назад +10

      Knowing the problem involves binary search is the hardest part, generally look for clues like minimize maximize, maybe this helps, recent div3 E question involved binary search, and I was so happy it clicked to me. dopamine.

    • @snehil3209
      @snehil3209 Год назад +1

      That interview problem right🤔

    • @aarushiagarwal8938
      @aarushiagarwal8938 Год назад +2

      @@shri9229 thanks for the tip hoping to apply soon

  • @adityarupda1795
    @adityarupda1795 Год назад +11

    For 10th trick in java there is a similar function to count setbit
    For integer x it is Integer.bitCount(x) and for long it is Long.bitCount(x).

  • @kapilpatel9379
    @kapilpatel9379 Год назад +1

    Watched half of it and liked it, let me come back when I finish the basic DSA part

  • @ayushmishra6575
    @ayushmishra6575 Год назад +2

    I knew the first one ,but others are also cool as hell. Really like this video.

    • @farj3946
      @farj3946 Год назад

      hell is not cool btw

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

    This is really insane.Really helpful.

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

    Very useful video..I felt bitwise operations as tough topic. You have made it easy now. Thank u

  • @shashwatyadav09
    @shashwatyadav09 Год назад +12

    Please upload videos like this, a few tips and tricks in competitive programming and related techniques.

  • @abhinavgupta4778
    @abhinavgupta4778 Год назад +2

    17:08 actually, can swap two numbers without using temporary variable without xor.
    suppose two numbers x and y
    now let x = x-y
    y = y+x ( y + x-y = x)
    x = y - x = (x - x +y)
    x and y are now swapped.

    • @adityarupda1795
      @adityarupda1795 Год назад +2

      You can also write
      X = X+Y;
      Y = X-Y;
      X = X-Y;

    • @abhinavgupta4778
      @abhinavgupta4778 Год назад +1

      @@adityarupda1795 yes 👍

    • @codecorn8030
      @codecorn8030 Год назад +3

      see the problem with this is overflow issue
      X+Y can sometimes be more than "long long int "
      say we needed to swap
      X = 1e18 and Y = 1e18 (yeah ik its same...but think user input was this and u dint knw both are same)
      now ur code fails
      ^ code works fine for all
      there are * and / to do the same thing u did
      but ^ way is best and can handel all the cases

  • @yerramarun2K05
    @yerramarun2K05 11 месяцев назад +1

    Everything😊😊

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

    Great explanation, in simple examples

  • @aveermukherjee6419
    @aveermukherjee6419 Год назад +34

    the bin function looks like sin function 😂

    • @appdipodu
      @appdipodu 11 месяцев назад

      🤣🤣 And, & looks like a yoga position 😉

    • @Its.abhi..
      @Its.abhi.. 10 месяцев назад

      Bc meri toh fatt gayi thi , sin kabse agya

  • @krsnasameer
    @krsnasameer Год назад

    Really great video, mad respect for you!

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

    great sir thank you

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

    awesome video, thanks!

  • @shreyashpatil4262
    @shreyashpatil4262 Год назад +2

    Please make more videos like DIFFERENCE ARRAY TECHNIQUE, (yesterday's vid), i have stuck many times on same type of questions/ questions which use similar concepts and we are just unable to optimise the code... also thanks for everything

  • @git_blasters
    @git_blasters 7 месяцев назад

    It was really helpful. Thank you for such a video and good explanation👍👍.

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

    Helpful 👌

  • @amimultamim9893
    @amimultamim9893 7 месяцев назад

    Thanks a lot! this was very helpful.

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

    thank you for this video sir....

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

    I just now saw this video . I am regretting why I didn't see this before 😢. It's really useful. Thank you .

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

    Nothing to say about you,,you are awesome

  • @flocela
    @flocela Год назад

    Thanks. Super good!

  • @priyamagarwal3469
    @priyamagarwal3469 Год назад +2

    Last few tricks are amazing.
    Can we have a video on Number theory tricks that can be very useful. I stuck a lot on basic Number Theory problems.

    • @aayushanand8285
      @aayushanand8285 Год назад +2

      Trick 9 is garbage. Use this identity only if you encounter rhs part somewhere, to transform back to A + B.
      How can a simple arithmetic operation which even has a cpu instruction for it (ie. ADD instruction), be slower than writing some hob nob like (A^B)+2*(A&B) or (A|B)+(A&B). Priyansh is trying to say us that we should opt for an operation consisting of a bitwise or, and, along with an "add" to replace a simple ADD operation. How idiotic isn't it. For the first identity, it also needs you to multiply with two as well. Simply slow because instead of doing one operation we're doing 3-4 extra operations.

    • @aayushanand8285
      @aayushanand8285 Год назад

      Just if something works mathematically, doesn't mean that going that alternative route is going to be faster. This is only learned through practice, and people who are aware of this, won't give out such tips without a 'gotcha'. Giving this information is good. But it should also be accompanied by a disclaimer that using this would make your code slow instead of fast. He says this in the intro of this video that bitwise operations are faster. People are going to take his word for it due his immense influence and popularity, but this is wrong.

    • @aayushanand8285
      @aayushanand8285 Год назад

      According to my benchmarks
      The first version is the most slow, (140796ns)
      2nd version is a bit better than the first, but still slow, (1146ns)
      3rd version is the fastest of all of them (108ns)

    • @aayushanand8285
      @aayushanand8285 Год назад

      Here is the benchmark code

    • @aayushanand8285
      @aayushanand8285 Год назад +1

      #include
      #include
      int Add1(int a, int b);
      int Add2(int a, int b);
      int AddNormal(int a, int b);
      constexpr int numIters = (int) 1e9;
      class Timer
      {
      public:
      using NanoSeconds = std::chrono::nanoseconds;
      public:
      Timer(std::chrono::nanoseconds& runtimeRef, bool shouldAlsoPrint = false)
      :m_RuntimeRefToReturn(runtimeRef)
      ,m_ShouldPrint(shouldAlsoPrint)
      {
      start = std::chrono::system_clock::now();
      }
      ~Timer()
      {
      m_RuntimeRefToReturn = std::chrono::system_clock::now() - start;

      if (m_ShouldPrint)
      std::cout

  • @aviraljoshi-vx9kq
    @aviraljoshi-vx9kq 6 месяцев назад

    very nice explanation

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

    NICE SUPER EXCELLENT MOTIVATED

  • @harshitgupta2909
    @harshitgupta2909 Год назад

    bro i am your big fan i love you bro i like your coding skills and i follow you too

  • @HarsithR-j6t
    @HarsithR-j6t Год назад

    Awesome tricks!

  • @HarshitGupta-i7s
    @HarshitGupta-i7s 4 месяца назад

    Great video !

  • @thulthula_diddy
    @thulthula_diddy Год назад

    Thank you

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

    please post pt2 !!

  • @World-Of-Mr-Motivater
    @World-Of-Mr-Motivater Год назад

    Very useful one

  • @MMNayem-dq4kd
    @MMNayem-dq4kd Год назад

    Thank you vaiya.

  • @kirtisingh-p4q
    @kirtisingh-p4q 11 месяцев назад

    Hi, a very popular problem with bitwise is when all the numbers appear 3 times except 1, can you please make a detailed video for the same.
    Thanks!

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

    Which note taking apps do you use???

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

    thanks:)

  • @dreamer707
    @dreamer707 Год назад

    awesome man

  • @Sandy-px1fz
    @Sandy-px1fz Год назад

    in the third trick the last one i didnt understand what you said and how it write you mean not of all the bits but how?

  • @KARTHIKEYANS-x9p
    @KARTHIKEYANS-x9p Год назад

    Man the last one was new to me found it more usefull.Try to make more usefull videos ilke this.

  • @depressd-boy.
    @depressd-boy. 2 месяца назад

    i need 1st vedio link

  • @shri9229
    @shri9229 Год назад

    23:02 eureka moment for me.

  • @susmithat881
    @susmithat881 7 месяцев назад

    A+b trick again you are doing addition after or and & operations

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

    Guys in trick 06 use XOR instead od AND beacuse it gives you a wrong swape here is an example: Initial values:
    X = 2 (binary: 10)
    Y = 3 (binary: 11)
    X = X AND Y (10 AND 11):
    X = 10 AND 11 = 10 (decimal: 2)
    Y = X AND Y (10 AND 11):
    Y = 10 AND 11 = 10 (decimal: 2)
    X = X AND Y (10 AND 10):
    X = 10 AND 10 = 10 (decimal: 2)
    see its not swaping correctly! but when you use XOR it will swape the values perfectly!

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

      bro watch the video once again where he told you to use AND. he is only using XOR in all three steps

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

    I liked trick 8 more than others

  • @ayushjaiswal2020
    @ayushjaiswal2020 Год назад

    xor swap trick ques was asked in ICPC Amritapuri Qualifiers.

    • @codecorn8030
      @codecorn8030 Год назад

      really ?
      which year ??
      if u are talking abt XOR sort of 2022
      nah bro...that question was completely diff 😂😂

    • @ayushjaiswal2020
      @ayushjaiswal2020 Год назад

      @@codecorn8030 i did it my xor swap approach only.

  • @ChotuSingh-em4qm
    @ChotuSingh-em4qm 9 месяцев назад +1

    bro try to run one program on each trick as an example

  • @icyesports803
    @icyesports803 Год назад

    When will tle eliminator next batch is going to start???

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

    8:04 The formula to toggle the Kth bit is x^(1

  • @phoenix_1_3
    @phoenix_1_3 Год назад +1

    When does TLE eliminators batch start after September? I'd like to join after my placements
    Checking if a number is a power of 2
    Swapping two numbers
    Manipulating kth bit
    Counting set bits
    These were super useful to me. Thanks a lot vro !
    Btw, whenever I give contests, I feel really hard to solve Bit manipulation problems. I can't even build up the intuition. Can you post long video about Bit manips covering the stuff needed for CP other than basics? It'd be really helpful

  • @kiranc-dc2wc
    @kiranc-dc2wc 3 месяца назад

    very good explanation easy to understand but inside video is annoying

  • @nurulkarim1416
    @nurulkarim1416 11 месяцев назад +1

    same video with animation on Log2base2.

  • @vishnu4647
    @vishnu4647 Год назад +1

    where is part1??

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

    I think I might actually win my competition now! best trick were: odd or even, and divide and multiply with 2^k

  • @rudranshverma861
    @rudranshverma861 7 месяцев назад

    Trick 8 is not exactly correct, and wont be good to use for some problems because it will return unexpected result if x is neither a nor b.

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

    Sin😂😂😂bin

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

    Wan zero wan hahahhaa

  • @AbdulMoeed-h1t
    @AbdulMoeed-h1t 2 месяца назад

    can you just slow down a bit? smh

  • @mmanikandan7125
    @mmanikandan7125 11 месяцев назад

    Why can't I understand anything

    • @RISHABH-VERMA
      @RISHABH-VERMA 5 месяцев назад

      Maybe you should start using bit operations a lil more in your code

  • @harshitgupta2909
    @harshitgupta2909 Год назад

    bro i am your big fan i love you bro i like your coding skills and i follow you too