Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6598301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:19:42+00:00 2026-05-25T18:19:42+00:00

For some base. Base 1 even. Some sort of complex substitution -ing. Also, and

  • 0

For some base. Base 1 even. Some sort of complex substitution -ing.

Also, and of course, doing this is not a good idea in real life production code. I just asked out of curiosity.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:19:43+00:00Added an answer on May 25, 2026 at 6:19 pm

    You can relatively easy write macro which adds two integers in binary. For example – macro which sums two 4-bit integers in binary :

    #include "stdio.h"
    
    // XOR truth table
    #define XOR_0_0 0
    #define XOR_0_1 1
    #define XOR_1_0 1
    #define XOR_1_1 0
    
    // OR truth table
    #define OR_0_0 0
    #define OR_0_1 1
    #define OR_1_0 1
    #define OR_1_1 1
    
    // AND truth table
    #define AND_0_0 0
    #define AND_0_1 0
    #define AND_1_0 0
    #define AND_1_1 1
    
    // concatenation macros
    #define XOR_X(x,y) XOR_##x##_##y
    #define   OR_X(x,y) OR_##x##_##y
    #define  AND_X(x,y) AND_##x##_##y
    #define OVERFLOW_X(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4) OVERFLOW_##rc1 (rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4)
    
    // stringification macros
    #define STR_X(x) #x
    #define STR(x) STR_X(x)
    
    // boolean operators
    #define XOR(x,y) XOR_X(x,y)
    #define   OR(x,y) OR_X(x,y)
    #define  AND(x,y) AND_X(x,y)
    
    // carry_bit + bit1 + bit2
    #define BIT_SUM(carry,bit1,bit2) XOR(carry, XOR(bit1,bit2))
    // carry_bit + carry_bit_of(bit1 + bit2)
    #define CARRY_SUM(carry,bit1,bit2) OR(carry, AND(bit1,bit2))
    
    // do we have overflow or maybe result perfectly fits into 4 bits ?
    #define OVERFLOW_0(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4) SHOW_RESULT(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4)
    #define OVERFLOW_1(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4) SHOW_OVERFLOW(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4)
    
    // draft-horse macros which performs addition of two 4-bit integers
    #define ADD_BIN_NUM(a1,a2,a3,a4, b1,b2,b3,b4) ADD_BIN_NUM_4(0,0,0,0, 0,0,0,0, a1,a2,a3,a4, b1,b2,b3,b4)
    #define ADD_BIN_NUM_4(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4) ADD_BIN_NUM_3(rc1,rc2,rc3,AND(CARRY_SUM(0,a4,b4),OR(a4,b4)), rb1,rb2,rb3,BIT_SUM(0,a4,b4), a1,a2,a3,a4, b1,b2,b3,b4)
    #define ADD_BIN_NUM_3(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4) ADD_BIN_NUM_2(rc1,rc2,AND(CARRY_SUM(rc4,a3,b3),OR(a3,b3)),rc4, rb1,rb2,BIT_SUM(rc4,a3,b3),rb4, a1,a2,a3,a4, b1,b2,b3,b4)
    #define ADD_BIN_NUM_2(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4) ADD_BIN_NUM_1(rc1,AND(CARRY_SUM(rc3,a2,b2),OR(a2,b2)),rc3,rc4, rb1,BIT_SUM(rc3,a2,b2),rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4)
    #define ADD_BIN_NUM_1(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4)      OVERFLOW(AND(CARRY_SUM(rc2,a1,b1),OR(a1,b1)),rc2,rc3,rc4, BIT_SUM(rc2,a1,b1),rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4)
    #define OVERFLOW(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4) OVERFLOW_X(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4)
    #define   SHOW_RESULT(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4) STR(a1) STR(a2) STR(a3) STR(a4) " + " STR(b1) STR(b2) STR(b3) STR(b4) " = " STR(rb1) STR(rb2) STR(rb3) STR(rb4)
    #define   SHOW_OVERFLOW(rc1,rc2,rc3,rc4, rb1,rb2,rb3,rb4, a1,a2,a3,a4, b1,b2,b3,b4) STR(a1) STR(a2) STR(a3) STR(a4) " + " STR(b1) STR(b2) STR(b3) STR(b4) " = overflow"
    
    void main()
    {
        printf("%s\n", 
            ADD_BIN_NUM(
                        0,0,0,1, // first  4-bit int
                        1,0,1,1) // second 4-bit int
                        );
    
        printf("%s\n", 
            ADD_BIN_NUM(
                        0,1,0,0, // first  4-bit int
                        0,1,0,1) // second 4-bit int
                    );
    
        printf("%s\n", 
            ADD_BIN_NUM(
                        1,0,1,1, // first  4-bit int
                        0,1,1,0) // second 4-bit int
                    );
    }
    

    This macro can be easily extended for addition of two 8-bit or 16-bit or even 32-bit ints.
    So basically all that we need is token concatenation and substitution rules to achieve amazing results with macros.

    EDIT:
    I have changed formating of results and more importantly – I’ve added overflow check.

    HTH!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large C++ code base that I'm doing some refactoring on where
When I declare some base styles for my site I have used to do
I'm trying to extend some base classes in Python: class xlist (list): def len(self):
How do I apply 'use base' in Perl to inherit subs from some base
I am in a project where we are starting refactoring some massive code base.
FxCop must base itself on some internal dictionary. Does it use Word dictionaries if
As part of the base class for some extensive unit testing, I am writing
Some background info; LanguageResource is the base class LanguageTranslatorResource and LanguageEditorResource inherit from LanguageResource
I have a base class vehicle and some children classes like car, motorbike etc..
Assume some domain and view objects (that have no common base class) public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.