please any one send a program to "addition and subtraction of two SIGNED BINARY NUMBERS by using 2'S COMPLEMENT" it is urgent

Dani AI

Generated

asked for a C program to perform addition and subtraction of signed binary numbers using 2's complement. The clean, portable approach is: pick a fixed bit width n (8/16/32), represent both operands as n-bit two's complement values (sign-extend if inputs have different lengths), do subtraction as addition of the two's complement of the subtrahend (A - B = A + (~B + 1)), mask the raw sum to n bits, and interpret the masked n-bit result as a signed value (if sign bit set, subtract 2^n). See Two's complement for the representation details.

C-specific tips: perform bit math with unsigned fixed-width types from <stdint.h> (uint8_t/uint16_t/uint32_t) and mask to n bits to avoid undefined signed overflow. Detect overflow for addition by checking whether the two operands share the same sign and the result has a different sign; for subtraction, overflow happens when the operands have different signs and the result's sign differs from the minuend. Signed overflow is undefined in C, so using unsigned arithmetic plus masking is the safe route—see .

's comment about homework and urgency is relevant: implementing a small routine is short and avoids cheating. 's reminder to ask clearly also matters: specify bit width, input format (strings or pre-parsed integers), and whether to show raw n-bit results or human-readable signed values. Common pitfalls: wrong width or missing sign-extension, misinterpreting final carry as overflow, and relying on platform-specific signed layouts.

Recommended Answers

All 2 Replies

If it's urgent, won't it be faster to write it yourself than wait for someone to send you something that may or may not work like you want? Also, if it's urgent, it's probably homework, and using someone else's code is cheating.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.