Bit Manipulation

แชร์
ฝัง
  • เผยแพร่เมื่อ 17 พ.ย. 2024

ความคิดเห็น • 54

  • @aronpop1447
    @aronpop1447 6 ปีที่แล้ว +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 ปีที่แล้ว +1

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

  • @griffintoal4410
    @griffintoal4410 7 ปีที่แล้ว +52

    im a bit lost :)

  • @eirikolsnes
    @eirikolsnes 7 ปีที่แล้ว +1

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

  • @arunsiddharth5184
    @arunsiddharth5184 7 ปีที่แล้ว +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;
    }

  • @HanifCarroll
    @HanifCarroll 4 ปีที่แล้ว +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

  • @LostInNeverland128
    @LostInNeverland128 5 ปีที่แล้ว +8

    ice town costs ice clown his town crown

    • @UdaraAlwis
      @UdaraAlwis 4 ปีที่แล้ว

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

  • @Paradise-kv7fn
    @Paradise-kv7fn 5 ปีที่แล้ว +1

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

    • @animodium2670
      @animodium2670 3 ปีที่แล้ว

      This can be simplified like so:
      return (1

  • @sergioropo3019
    @sergioropo3019 6 ปีที่แล้ว +1

    Very informative and well done. Thank you.

  • @adibhargava7055
    @adibhargava7055 3 ปีที่แล้ว

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

  • @tauseef10s
    @tauseef10s 8 ปีที่แล้ว +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

  • @ahmedramzy892
    @ahmedramzy892 11 หลายเดือนก่อน

    great work 🥰

  • @oliviazhai1831
    @oliviazhai1831 2 ปีที่แล้ว

    This is very helpful!!! Thanks

  • @_sayan_roy_
    @_sayan_roy_ 7 ปีที่แล้ว

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

    • @_sayan_roy_
      @_sayan_roy_ 7 ปีที่แล้ว

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

  • @olivergreen6436
    @olivergreen6436 4 ปีที่แล้ว

    Thanks it's very informative

  • @satadhi
    @satadhi 7 ปีที่แล้ว +8

    0:18 i mean seriously !

    • @satadhi
      @satadhi 7 ปีที่แล้ว +1

      btw cool videos

  • @mr.anonymous6098
    @mr.anonymous6098 2 ปีที่แล้ว

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

  • @MAA-op4gw
    @MAA-op4gw ปีที่แล้ว

    Thank you so much.

  • @cschipg4688
    @cschipg4688 2 ปีที่แล้ว +1

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

    • @maheshj01
      @maheshj01 ปีที่แล้ว

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

  • @panmacabre9895
    @panmacabre9895 ปีที่แล้ว

    that's freaking amazing

  • @yamamarques27
    @yamamarques27 2 ปีที่แล้ว

    //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 4 ปีที่แล้ว

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

  • @jagannathanrajagopalan2941
    @jagannathanrajagopalan2941 4 ปีที่แล้ว +2

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

    • @adibhargava7055
      @adibhargava7055 3 ปีที่แล้ว

      But what if the starting bit is a 1?

    • @ytdl
      @ytdl 3 วันที่ผ่านมา

      Because if you try to clear a bit that is 0 / already cleared it will return a 1.

  • @cristianrestrepolopez976
    @cristianrestrepolopez976 3 ปีที่แล้ว

    Thank you so much this video was extremely helpful :)

  • @TDLRest
    @TDLRest 4 ปีที่แล้ว +1

    thanks so much for this! you're awesome teacher

  • @abdullahnoman8618
    @abdullahnoman8618 7 ปีที่แล้ว +2

    you could use the xor operator in clearing the bit

  • @pedroalonsoms
    @pedroalonsoms 2 ปีที่แล้ว

    such a good video

  • @AviPars
    @AviPars 2 ปีที่แล้ว

    How would I flip all the bytes ?

  • @iXNomad
    @iXNomad 2 ปีที่แล้ว

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

  • @ashishpatel0720
    @ashishpatel0720 8 ปีที่แล้ว

    very good video
    thank you very much

  • @nishajakhar5867
    @nishajakhar5867 6 ปีที่แล้ว

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

  • @aronpop1447
    @aronpop1447 6 ปีที่แล้ว

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

    • @aaryandhakal6098
      @aaryandhakal6098 3 ปีที่แล้ว

      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

  • @gauravsoni5974
    @gauravsoni5974 6 ปีที่แล้ว

    What is the mask in terms of bit manipulation?

    • @dtm7743
      @dtm7743 3 ปีที่แล้ว

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

  • @vaibhavtiwari6992
    @vaibhavtiwari6992 4 ปีที่แล้ว

    THIS IS GOOD FR SOMEONE WHO ALREADY KNOWS THIS STUFF.

  • @cadupoles
    @cadupoles 7 ปีที่แล้ว

    Thank you

  • @tushararora347
    @tushararora347 7 ปีที่แล้ว

    What's that sound at 22:41?

    • @sergioropo3019
      @sergioropo3019 6 ปีที่แล้ว

      Obviously is cartoon character exclamation.

  • @gigamilk6981
    @gigamilk6981 3 ปีที่แล้ว

    This guys pretty cute

    • @gigamilk6981
      @gigamilk6981 3 ปีที่แล้ว

      I take it back hes very cute

  • @jonathancao1396
    @jonathancao1396 7 ปีที่แล้ว

    i love you