Bit Manipulation

Sdílet
Vložit
  • čas přidán 22. 07. 2024

Komentáře • 53

  • @aronpop1447
    @aronpop1447 Před 6 lety +20

    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.

  • @larisabadescu2213
    @larisabadescu2213 Před rokem

    this is the only video that actually helped me understand bit manipulation, thank you a lot

  • @eirikolsnes
    @eirikolsnes Před 7 lety +1

    Thank you!
    Nice to see how giving all students time to respond resulted in valuable input.

  • @sergioropo3019
    @sergioropo3019 Před 6 lety +1

    Very informative and well done. Thank you.

  • @griffintoal4410
    @griffintoal4410 Před 6 lety +52

    im a bit lost :)

  • @cristianrestrepolopez976

    Thank you so much this video was extremely helpful :)

  • @HanifCarroll
    @HanifCarroll Před 4 lety +2

    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

  • @olivergreen6436
    @olivergreen6436 Před 3 lety

    Thanks it's very informative

  • @LostInNeverland128
    @LostInNeverland128 Před 5 lety +8

    ice town costs ice clown his town crown

    • @UdaraAlwis
      @UdaraAlwis Před 4 lety

      yessss! this exactly! I believe only a few will get this reference xD

  • @adibhargava7055
    @adibhargava7055 Před 3 lety

    You can also say floor function instead of round down to negative infinity @12:05

  • @arunsiddharth5184
    @arunsiddharth5184 Před 6 lety +4

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

  • @AviPars
    @AviPars Před 2 lety

    How would I flip all the bytes ?

  • @panmacabre9895
    @panmacabre9895 Před rokem

    that's freaking amazing

  • @mr.anonymous6098
    @mr.anonymous6098 Před 2 lety

    Great video! Highly recommend it. Wish he was my teacher

  • @MAA-op4gw
    @MAA-op4gw Před rokem

    Thank you so much.

  • @oliviazhai1831
    @oliviazhai1831 Před rokem

    This is very helpful!!! Thanks

  • @ahmedramzy892
    @ahmedramzy892 Před 8 měsíci

    great work 🥰

  • @aronpop1447
    @aronpop1447 Před 6 lety

    Couldnt have you used the ^ operator for clearing the bits? Exact same code except return x ^ mask?

    • @aaryandhakal6098
      @aaryandhakal6098 Před 3 lety

      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

  • @Paradise-kv7fn
    @Paradise-kv7fn Před 5 lety +1

    A easier way for the problem of checking a bit would be
    return (1

  • @cschipg4688
    @cschipg4688 Před 2 lety +1

    This guy is so chill presenting. how do you get that relaxed mate

    • @maheshjamdade1
      @maheshjamdade1 Před rokem

      My two cents
      - Think of it like a normal conversation
      - and keep your focus on the topic

  • @ashishpatel0720
    @ashishpatel0720 Před 8 lety

    very good video
    thank you very much

  • @TDLRest
    @TDLRest Před 4 lety +1

    thanks so much for this! you're awesome teacher

  • @_sayan_roy_
    @_sayan_roy_ Před 7 lety

    24:12 , More intuitive for me is:-
    int modBit(x,pos,state){
    mask1 = 1

    • @_sayan_roy_
      @_sayan_roy_ Před 7 lety

      +l0ad1 Thanks a lot... :) Now,I do.

  • @abdullahnoman8618
    @abdullahnoman8618 Před 7 lety +2

    you could use the xor operator in clearing the bit

  • @jagannathanrajagopalan2941

    why not use XOR to clear a bit? any reason? i mean 1

  • @gauravsoni5974
    @gauravsoni5974 Před 6 lety

    What is the mask in terms of bit manipulation?

    • @dtm7743
      @dtm7743 Před 3 lety

      predefined set of bits used to pick and choose which specific bits will be modified by subsequent operations

  • @cadupoles
    @cadupoles Před 7 lety

    Thank you

  • @pedroalonsoms
    @pedroalonsoms Před rokem

    such a good video

  • @iXNomad
    @iXNomad Před rokem

    Why do you use -state if you can just write:
    x = (x & ~(1

  • @tauseef10s
    @tauseef10s Před 7 lety +4

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

  • @nishajakhar5867
    @nishajakhar5867 Před 6 lety

    You look damn awesome....And even loved your teaching style..

  • @tushararora347
    @tushararora347 Před 6 lety

    What's that sound at 22:41?

  • @satadhi
    @satadhi Před 7 lety +8

    0:18 i mean seriously !

    • @satadhi
      @satadhi Před 7 lety +1

      btw cool videos

  • @yamamarques27
    @yamamarques27 Před 2 lety

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

  • @II_superluminal_II
    @II_superluminal_II Před 4 lety

    now i get brainfuck holy shit, brainfuck is fast as fuck and now I know why.......

  • @vaibhavtiwari6992
    @vaibhavtiwari6992 Před 4 lety

    THIS IS GOOD FR SOMEONE WHO ALREADY KNOWS THIS STUFF.

  • @jonathancao1396
    @jonathancao1396 Před 6 lety

    i love you

  • @gigamilk6981
    @gigamilk6981 Před 3 lety

    This guys pretty cute