Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Addition Without Plus Operator

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Addition Without Plus Operator. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

TCS BaNCS services may need low-level arithmetic logic when direct arithmetic operators are restricted. Implement integer addition without using the + operator or any equivalent arithmetic shortcut.

Use bitwise XOR to combine bits without carry, and bitwise AND followed by a left shift to calculate the carry. Continue until no carry remains.

Formal Specification

Implement add_without_plus(a, b):

  • Input: Two signed 32-bit integers a and b.
  • Output: Their mathematical sum, represented as a signed 32-bit integer.
  • Do not use +, sum, or string-based conversion to perform addition.
  • Bitwise operators such as ^, &, <<, and ~ are allowed.
  • Results follow signed 32-bit two's-complement behavior, including wraparound.

Constraints

  • -2^31 <= a, b <= 2^31 - 1
  • The result follows signed 32-bit two's-complement behavior
  • The implementation cannot use the plus operator
  • Only bitwise operations and control flow may implement the addition

Function Signature

def add_without_plus(a, b):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output