Add Two Numbers Without The "+" Sign (Bit Shifting Basics)

แชร์
ฝัง
  • เผยแพร่เมื่อ 22 ก.พ. 2019
  • Free 5-Day Mini-Course: backtobackswe.com
    Try Our Full Platform: backtobackswe.com/pricing
    📹 Intuitive Video Explanations
    🏃 Run Code As You Learn
    💾 Save Progress
    ❓New Unseen Questions
    🔎 Get All Solutions
    Question: Add two integer values without using the "+" operator.
    ++++++++++++++++++++++++++++++++++++++++++++++++++
    HackerRank: / @hackerrankofficial
    Tuschar Roy: / tusharroy2525
    GeeksForGeeks: / @geeksforgeeksvideos
    Jarvis Johnson: / vsympathyv
    Success In Tech: / @successintech
  • วิทยาศาสตร์และเทคโนโลยี

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

  • @BackToBackSWE
    @BackToBackSWE  5 ปีที่แล้ว +41

    Table of Contents
    The Problem Introduction 0:00 - 0:51
    A Way To Think About Number Bases 0:51 - 3:56
    Back To The Problem At Hand 3:56 - 4:16
    Let's Add Some Numbers (Decimal, Base 10) 4:16 - 4:59
    Let's Add Some Numbers (Binary, Base 2) 4:59 - 7:40
    Bit Shifting Operators: The 'AND' -> '&' 7:40 - 9:41
    Bit Shifting Operators: The 'XOR' -> '^' 9:41 - 11:36
    Bit Shifting Operators: The 'Left Shift' -> '

    • @danavram8437
      @danavram8437 4 ปีที่แล้ว +6

      What do you mean? It also works on negative numbers (great explanation of the whole process btw :) ). I used this algorithm on leetcode in C++ (and it has to work with negative numbers as well). The only catch is that I had to convert the carry to unsigned when doing the left bitshift (which wraps the bits around with 1111....11 (32 bits))
      int getSum(int a, int b) {
      while(b)
      {
      int carry = a & b; // get all carry bits (all 1&1s)
      a = a^b; // sum of disjoint bits (everything except what needs to be carried)

      // shift carry by one, as this is how a regular sum operation works
      // the carry is moved to the next position
      b = (unsigned)carry

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

      yeah

    • @arunprakash1101
      @arunprakash1101 4 ปีที่แล้ว +14

      Hey !! This explanation is excellent! But I can't see the code for this problem in the description! Is it moved?

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

      Arun Prakash same here

    • @anjalibelani5101
      @anjalibelani5101 3 ปีที่แล้ว +7

      Thank you for these videos. However, I dont see the code in the description in any of your videos. Am I missing something?

  • @minhazulislam4682
    @minhazulislam4682 3 ปีที่แล้ว +77

    dude, you need to come back and teach us.
    the world needs more people like you, blackpenredpen, the organic chemistry teacher, 3blue1brown, nick white, tech with tim, jenny it and many more.

    • @albertjtyeh53
      @albertjtyeh53 3 ปีที่แล้ว +1

      is he working at FAANG now?

    • @MrACrazyHobo
      @MrACrazyHobo 3 ปีที่แล้ว +1

      @@albertjtyeh53 Back To Back SWE is at twitter!

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

      Corey Schafer and Sentdex are great too

  • @baichen6449
    @baichen6449 4 ปีที่แล้ว +32

    your explanation really helps people who were totally lost, like me.

  • @anniewiley1425
    @anniewiley1425 2 ปีที่แล้ว +3

    Hey man I just wanted to say I think these videos are immensely helpful. The way you explain your thought process on these complex but tiny subjects rashly helps a person change the way they think about code, not to mention learning the lingo of a SWE.
    Also please don't ever lose the awkward endings and outtakes.

  • @null5245
    @null5245 5 ปีที่แล้ว +124

    u r a good teacher bro!
    Actually i came here because of your comment in leetcode lol :)

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +2

      nice

    • @angelanayiga4060
      @angelanayiga4060 4 ปีที่แล้ว +6

      @@BackToBackSWE I do agree you should make comments on all leet code questions you've solved! That's how I landed on B2B SWE!

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

      @@angelanayiga4060 hey

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

      @@angelanayiga4060 Now because of your comment I landed on B2B which I didn't even know.

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

      Harsha G I’m really glad you landed on it and I hope it helps you the way it helped me 🤗🙏🏾

  • @SR-we1vl
    @SR-we1vl 3 ปีที่แล้ว +3

    Whenever I get stuck on a problem and see if your video is there, I'm relieved! Thank you man!

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

    I took compsci and learned this but could never explain to my friend who couldn’t grasp it. She struggled the whole time.
    This is such a great explanation and you are an excellent teacher.

  • @ShuangyiHu
    @ShuangyiHu 3 หลายเดือนก่อน

    You are such a good teacher! Thanks for breaking down the basics and emphasizing the details!

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

    The best teaching that I have seen in my life till now is yours .

  • @CodingForRealLife
    @CodingForRealLife 4 ปีที่แล้ว +46

    Now I understand a lot of techniques that I wasn't able to understand before by following your channel, keep it up!

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

    This is the ultimate guide to this hell of bits manipulation. Thank you so much.

  • @vS-xv7vm
    @vS-xv7vm 3 ปีที่แล้ว +5

    Explanation is so good that we won't need to write code for this in interview - just a dry-run with example would do.

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

    Genuinely explained. Excellent, keep doing your work. You`re a great teacher.

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

    You are awesome! Your presentation is so clear and easy to understand!

  • @AlexanderFilippov-ua
    @AlexanderFilippov-ua 4 ปีที่แล้ว +4

    This is the best explanation I've ever seen! Thanks a lot!!!

  • @hoboshenanigans
    @hoboshenanigans 4 ปีที่แล้ว +19

    Dude keep it up, i've learned more from you then my attempt at graduate school lol

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

    I have spent literal years glossing over bitshifting because no explanation could come close to me understanding it. I now have at least a basic knowledge and a starting place. Thank you!

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

    Your explanations are always so clear and engaging.

  • @PiBiNi
    @PiBiNi 3 ปีที่แล้ว +1

    Loved the way you explained even the basics. Thank you.

  • @harinijeyaraman8789
    @harinijeyaraman8789 3 ปีที่แล้ว +2

    This was super helpful !! You're amazing at teaching !

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

    Great video man ✊🏾

  • @mallikarjunpidaparthi
    @mallikarjunpidaparthi 3 ปีที่แล้ว +1

    Wow. You made it so simple. Thank you sir.

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

    Oh wow! Cool, this was a nice refresher. Great video

  • @haleygu2171
    @haleygu2171 4 ปีที่แล้ว +14

    Faced this problem in an interview with Qualcomm ... I wish I could see this video earlier to come up with bit manipulation solution :( Thanks a lot for clear explaination!

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

      nice

    • @prithazz
      @prithazz 4 ปีที่แล้ว +3

      This is a stupid interview question to ask.. honestly, they don't deserve you.

    • @snlagr
      @snlagr 3 ปีที่แล้ว +1

      @@prithazz maybe they wanted to check low level understanding

  • @alex-gz7ud
    @alex-gz7ud 3 ปีที่แล้ว

    Thank you for the explanation!!
    You are awesome!!!

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

    Thanks a lot.Ur explaination really cleared my doubt....

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

    Amazing explanation, much thanks !

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

    Thank you very much. Finally, I got the concept!

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

    Excellent teacher. Excellent!!

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

    Explanation is awesome bro

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

    Thank you for the superb explanation!

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

    So clearly explained, i can't thank enough. 👍

  • @justin.j4440
    @justin.j4440 9 หลายเดือนก่อน

    Nicely explained!

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

    You are extremely talented at teaching this stuff, I have struggled with bitwise operations for a while now, however after watching your explanation I was able to implement this myself, which is a major break through.
    Thank you for this video!

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

    Great video. Speaking the entire process through like the computer solidified it for me.

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

    That's simply Great. I appreciate your work. Thanks for the explanation ♥️♥️

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

    Amazing explanation!

  • @ilshatt
    @ilshatt 10 หลายเดือนก่อน

    Very nice explanation!

  • @user-su1pu2bm4y
    @user-su1pu2bm4y 2 ปีที่แล้ว

    My Brother, since college back in 1999, I haven't had to use it. But, you have refreshed my way of thinking. Thanks and continue to progress forward!!!!

  • @CHIRANJIBNANDY1
    @CHIRANJIBNANDY1 4 ปีที่แล้ว +7

    Bro...u r a genius.... thanks for making me prepared.

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

    this is so helpful thank you!

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

    this is so helpful. Thank you

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

    thanks for the explanation!

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

    Love you man. I'm in Graduate school & your videos have helped me so much.

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

    That's a pretty intuitive explanation here!

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

    Ive learnt alot today. Ive been struggling these days and i was almost burn out because of the amount of unfinished projects i had. Thank you

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

    Great video man

  • @jkim5118
    @jkim5118 5 ปีที่แล้ว +3

    I am always impressed with your videos. How do you choose the questions? It seems like you have a set of question that you plan to cover and I can feel that those questions are more relevant to actual skills that we need for interviews (not like brain teasers). Also, what's your plan after to cover all planned questions?

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +1

      I built a list of like 300 when I started this channel. But at some point I just started doing the most burning questions that I felt that I just HAD to try to cover.
      And as for plans after I have many questions covered...eh, I don't know. Here is my rough plan for this project: backtobackswe.com/plans
      I don't think any human can ever cover all questions. But they can cover all topics and concepts.
      This channel is far from the latter.

  • @siddhantsharma4011
    @siddhantsharma4011 7 หลายเดือนก่อน

    Glad I found this channel. Truly amazing 🎉

    • @BackToBackSWE
      @BackToBackSWE  7 หลายเดือนก่อน

      Happy Halloween 🎃 Thank you for your kind words, Siddhant! Let us know other topics we could cover! We'd love to offer you 50% Off our exclusive lifetime membership use the code SPOOKY50 - backtobackswe.com/checkout?plan=lifetime-legacy&discount_code=SPOOKY50

  • @maxxxxx93
    @maxxxxx93 5 ปีที่แล้ว +4

    very well explained solution!
    i think it’s worth noting that this only really works with positive integers

    • @BackToBackSWE
      @BackToBackSWE  5 ปีที่แล้ว +1

      Yeah, true, I'll add that to the notes.

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

    You are amazing! Thank you

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

    Best explanation I've seen. It helped me implement my C# program!
    Could you do a similar video for substraction without using arithmetic operators?

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

    This video helped a lot, thanks

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

    You my friend are amazing !! Thanks

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

    Awesome bro love all your videos

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

    That is an awesome explanation! Thanks!!!!

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

    Awesome explanation.. By the way once again thank you for ...

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

    Thank you, I'm learning so much from your lectures!

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

    Best explanation, so thank you, man!

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

    Excellent explanation, thank you!

  • @traveltechtaste41
    @traveltechtaste41 3 ปีที่แล้ว +1

    Thanks for explaining this so clearly

  • @Sarah-il5dr
    @Sarah-il5dr 4 ปีที่แล้ว +1

    You are my go to when comes to all questions!

  • @finiteimpulse3765
    @finiteimpulse3765 4 ปีที่แล้ว +5

    A short video like this about Two's complement would be great.

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

    This was a real cool and intuitive explanation! Thanks!

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

      Thank You, Glad you liked it.
      Do check out backtobackswe.com/platform/content
      and please recommend us to your family and friends :)

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

    love this channel!

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

      thanks buddy! try our 5 day free mini course for some awesome content - backtobackswe.com/

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

    Man, that's been interesting and fun to listen to you! Thanks! I got topic)

  • @Sat-Kabir-Saheb
    @Sat-Kabir-Saheb 5 ปีที่แล้ว +2

    You helped a lot , thank you for making such kind of videos.....

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

    you really helped with just simple concepts

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

    Excellent, you have put in lot of effort in explaining it, thanks appreciate it!! :)

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

    Amazing explanation !! Thanks for your effort

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

    Very good explanation..

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

    Thanks man!

  • @cozywanderer
    @cozywanderer 3 ปีที่แล้ว +1

    This was a fantastic explanation!!

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

    Awesome explanation covering from basics 🔥

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

    It was great. Thanks

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

    What an explanation man! thank you ton!!

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

    Totally understood from zero background. Thumbs up!

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

    Haven't seen a better explanation. Kudos.

  • @kevinkkirimii
    @kevinkkirimii 3 ปีที่แล้ว +1

    I start so well, then I just get lost. I have to find a way to get this concepts in my head permanently.

  • @joydeep-halder
    @joydeep-halder 2 ปีที่แล้ว

    wow. teaching in so simple way. Amazing!

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

      Thank you, glad you liked it 😀
      Do check out backtobackswe.com/platform/content
      and please recommend us to your family and friends 😀

  • @abcdeereijgfhd3215
    @abcdeereijgfhd3215 3 ปีที่แล้ว +1

    brilliant explanation~!

  • @ZhenyaVlasov
    @ZhenyaVlasov 4 ปีที่แล้ว +5

    Thanks for your vids. I found some inconsistencies with your explanation. At 11:48 you say that when we do shift, '1 gets shifted out'. However, it doesn't.
    As an example, take 0101 (5) and 1101(13)
    When shifting, we add 0 on the right side and if carry started with 1, it will grow as well. We can't loose the first 1 because just like other 1s in the carry, it specify an index where we need to carry over.
    In the provided github code, line 134, it says "1 got shifted out". Again, if we shift it, we will loose part of our answer.
    The reason why, at some point, 'carry' becomes 0 and we brake out of the loop, because on 3rd iteration after increasing the carry by one more digit, we will be performing '&' operation between 4 digit (0010) and 5 digit (10000). The result of it will be 00000.
    Result of the '^' operation will be 10010 (which, again, won't be possible if we lost that left most '1'.
    'carry

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

      Woah, a lot to parse, will need to jog my memory on this one. And nice, hey, I'm in Maryland rn so oof

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

    Great explanation

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

    Thank you! Helped me alot

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

    Man!!! This is so helpful!!!

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

    Thank you so much, this solve my wondering for years as not an engineer

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

    U R an excellent teacher 👍🏼thanks a lot ✔️

  • @lets_see_777
    @lets_see_777 5 ปีที่แล้ว +3

    best explanation, subbed

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

    Never saw a better explanation than this👏👏👏

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

    Never thought about this logic 😯😯.... Amazing 😍😍

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

    Very nicely explained, Sir

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

    this is really awesome. really loved it gr8 work!!!! all doubts are solved thanks..!!!!

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

    Using that ad to cover the redundant statement was brilliant lmao. I would've never noticed it was repeated if you didn't point it out honestly.

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

    thanks man, it really helped

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

    Excellent video.

  • @fredwooten14
    @fredwooten14 5 ปีที่แล้ว +1

    Great videos my brother!

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

    This guy was born to teach!

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

    I just started to study this stuff and my mind is like seeing the light

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

    Awesome explanation!

  • @FabioMontefuscolo
    @FabioMontefuscolo 5 ปีที่แล้ว +1

    Awesome! Thank you very much!!