Click here to Skip to main content
       

C / C++ / MFC

 
AnswerRe: Dynamic cast question Pinmemberfat_boy25-Jun-12 0:45 
QuestionSetDIBits and GetDIBits (Aarot.zip) PinmemberRabindra Laheri24-Jun-12 18:33 
AnswerRe: SetDIBits and GetDIBits (Aarot.zip) PinmemberRichard Andrew x6424-Jun-12 18:49 
GeneralRe: SetDIBits and GetDIBits (Aarot.zip) PinmemberRabindra Laheri24-Jun-12 19:39 
AnswerRe: SetDIBits and GetDIBits (Aarot.zip) PinmvpRichard MacCutchan24-Jun-12 22:22 
QuestionPrecompiled Headers PinmemberForNow24-Jun-12 11:36 
AnswerRe: Precompiled Headers PinmemberChris Losinger24-Jun-12 12:35 
AnswerRe: Precompiled Headers Pinmemberjschell25-Jun-12 9:45 
QuestionPut text data to position where cursor is blinking Pinmemberkhanhnd14058224-Jun-12 4:34 
QuestionWin32 local aplication using accessing mysql instaled in a site with easyphp ... PinmemberSNArruda23-Jun-12 13:02 
AnswerRe: Win32 local aplication using accessing mysql instaled in a site with easyphp ... Pinmembermarkkuk23-Jun-12 23:43 
QuestionBinary value Division PinmemberSyed umair shah23-Jun-12 0:48 
AnswerRe: Binary value Division PinmvpRichard MacCutchan23-Jun-12 2:14 
AnswerRe: Binary value Division PinmemberTopCoder2323-Jun-12 11:20 
#include <stdio.h>
#include <stdlib.h>
 
void load(int*, int*, int*);
void display(int*, int*, int*);
void compute(int*, int*, int*);
void adder(int*, int*);
void negate(int*);
 
void main()
{
    int i;
    int DIVISOR [5];
    int REMAINDER [5];
    int DIV_QUOT [4];   // DIVIDEND - QUOTIENT

//initialising registers with data
    load(DIVISOR, REMAINDER, DIV_QUOT);
 
    //Decorations.
    printf("  DIVISOR |\t     REMAINDER\t     DIVIDEND/QUOTIENT");
        printf("\n  --------|----------------");
            printf("---------------------------\n");
 
    display(DIVISOR, REMAINDER, DIV_QUOT);  //general display
        printf("\n  --------|----------------");
            printf("---------------------------\n");
    //Done with decorations.

    //computation
    for (i=0; i<4; i++)
    {
        compute(DIVISOR, REMAINDER, DIV_QUOT);  //compute 1st bit
        printf("\n  --------|----------------");
            printf("---------------------------\n");
    }
}
 

 

 
///////////////////////////////////////////////////////
// Computation
void compute(int DIVISOR[], int REMAINDER[], int DIV_QUOT[])
{
    int DIVISOR_Copy [5];
    bool neg;
 
    REMAINDER[1] = REMAINDER[2];
    REMAINDER[2] = REMAINDER[3];
    REMAINDER[3] = REMAINDER[4];
    REMAINDER[4] = DIV_QUOT[0];
    DIV_QUOT[0] = DIV_QUOT[1];
    DIV_QUOT[1] = DIV_QUOT[2];
    DIV_QUOT[2] = DIV_QUOT[3];
    DIV_QUOT[3] = 888;          //flag to put "?" sign

    display(DIVISOR, REMAINDER, DIV_QUOT);  //display
    printf("  |  <--- Shifting left...\n");
 

    // NOW LETS DETERMINE THE STATUS OF DIV_QUOT[3]
    // TO DO SO.... SUBSTRACT. (REMAINDER - DIVISOR) AND SEE
    // IF NEGATIVE, IT IS 0; AND RESTORE REMAINDER
    // IF POSITIVE IT IS 1 AND DO NOT RESTORE REMAINDER.

    /////////////////////////
    // SUBSTRACT operation HERE.
    // REMAINDER - DIVISOR  = ?
    //
    // first lets create and negate DIVISOR_Copy
    // an image of DIVISOR

    DIVISOR_Copy[0] = DIVISOR[0];
    DIVISOR_Copy[1] = DIVISOR[1];
    DIVISOR_Copy[2] = DIVISOR[2];
    DIVISOR_Copy[3] = DIVISOR[3];
    DIVISOR_Copy[4] = DIVISOR[4];
 
    negate(DIVISOR_Copy);
 
    adder(REMAINDER, DIVISOR_Copy);     // Addition (actually substraction)

    if (REMAINDER[0] == 1)          // Checking control bit.
                            // (overflow or neg)
        neg = 1;  /// Negative
    else
        neg = 0;  /// Positive

    display(DIVISOR, REMAINDER, DIV_QUOT);  //display

    if (neg)    // if the number was negative,
            // restore REMAINDER and assign 0 to D[3]
    {
        adder(REMAINDER, DIVISOR);  //Restoration
        DIV_QUOT[3] = 0;
        printf("  |  NEGATIVE. ? = 0\n");
    }
    else
    {
        DIV_QUOT[3] = 1;
        printf("  |  POSITIVE. ? = 1\n");
    }
    display(DIVISOR, REMAINDER, DIV_QUOT);  //display
}
 

///////////////////////////////////////////////////////
// Two's compliment.
void negate(int A[])
{
    int i;
    int ONE[5];
    ONE[0] = 0;
    ONE[1] = 0;
    ONE[2] = 0;
    ONE[3] = 0;
    ONE[4] = 1;
 
    for (i=0; i<=4; i++)
    {
        if(A[i] == 1)
            A[i] = 0;
        else
            A[i] = 1;
    }
    //ADD 1
    adder(A, ONE);
}
 

///////////////////////////////////////////////////////
// 4 bit adder.
void adder(int A[], int B[])
{
    int next, i, carry=0;
 
    for (i=4; i>=0; i--)    // 4, because we have sign control bit
    {
        next = 1;
        if (A[i]==0 && B[i]==0)     //00 case
        {
            A[i]= carry;
            carry = 0;
            next = 0;
        } // good.

        if (next)
        if (A[i]==0 && B[i]==1)     //01 case   - transfer
        {
            if (carry == 0)
                A[i] =1;
            else
                A[i]=0;
            next = 0;
        }
 
        if(next)
        if (A[i]==1 && B[i]==0)     //10 case   - transfer
        {
            if (carry == 0)
                A[i] =1;
            else
                A[i]=0;
            next = 0;
        }
 
        if(next)
        if (A[i]==1 && B[i]==1)     //11 case - generate
        {
            if (carry == 0)
            {
                A[i] =0;
                carry = 1;
            }
            else
                A[i]=1;
        next = 0;
        }
    }
}
 

 
///////////////////////////////////////////////////////
//displaying {OK}
void display(int A[], int B[], int C[])
{
    int i;
    printf(" ");
    for(i=1; i<=4; i++)
        printf(" %d", A[i]);
    printf(" |          ");
 
    for(i=1; i<=4; i++)
        printf(" %d", B[i]);
    printf("            ");
 
    for(i=0; i<=3; i++)
        if ((C[i] == 0) || (C[i] == 1))
            printf(" %d", C[i]);
        else
            printf(" ?");
}
 
///////////////////////////////////////////////////////
//function to load registers with data.  {OK}
void load(int A[], int B[], int C[])
{
 
    A[0] = 0;
    A[1] = 1;  //DIVISOR
    A[2] = 0;
    A[3] = 0;
    A[4] = 0;
 
    B[0] = 0;  // !!!!!!SIGN CONTROL BIT!!!!!!
    B[1] = 0;  // ALL ZEROS
    B[2] = 0;  // ALL ZEROS
    B[3] = 0;  // ALL ZEROS
    B[4] = 0;  // ALL ZEROS

 
    C[0] = 1;  //DIVIDEND / QUOT
    C[1] = 0;
    C[2] = 0;
    C[3] = 0;
}

Questionhow to do logic gates PinmemberSyed umair shah23-Jun-12 0:47 
AnswerRe: how to do logic gates PinmemberBrandon T. H.23-Jun-12 13:11 
Questionhow to do logiz gates PinmemberSyed umair shah23-Jun-12 0:45 
AnswerRe: how to do logiz gates PinmemberTopCoder2323-Jun-12 11:07 
QuestionWhat FTP pack can obtain the detail events? Pinmemberincludeh1022-Jun-12 22:36 
AnswerRe: What FTP pack can obtain the detail events? PinmemberJochen Arndt22-Jun-12 23:03 
QuestionOnly unist.isu gets installed by visual C++ install shield Pinmemberadityarao3122-Jun-12 20:30 
QuestionMFC UI Thread vs. Background Thread PinmemberRichard Andrew x6422-Jun-12 10:19 
AnswerRe: MFC UI Thread vs. Background Thread [modified] Pinmember«_Superman_»22-Jun-12 17:19 
SuggestionRe: MFC UI Thread vs. Background Thread PinmemberRoger Stoltz23-Jun-12 0:26 
GeneralRe: MFC UI Thread vs. Background Thread PinmemberRichard Andrew x6423-Jun-12 8:31 
AnswerRe: MFC UI Thread vs. Background Thread PinmemberVitaly Tomilov23-Jun-12 13:17 
AnswerRe: MFC UI Thread vs. Background Thread Pinmemberfat_boy23-Jun-12 23:55 
QuestionA Simple C++ Question. Pinmembersirama200422-Jun-12 4:19 
AnswerRe: A Simple C++ Question. PinmemberDavidCrow22-Jun-12 4:47 
GeneralRe: A Simple C++ Question. Pinmembersirama200426-Jun-12 0:51 
AnswerRe: A Simple C++ Question. PinmemberAlbert Holguin22-Jun-12 5:05 
AnswerRe: A Simple C++ Question. PinmemberMaximilien22-Jun-12 5:26 
AnswerRe: A Simple C++ Question. PinmemberChuck O'Toole22-Jun-12 8:21 
GeneralRe: A Simple C++ Question. Pinmemberjschell22-Jun-12 13:01 
AnswerRe: A Simple C++ Question. PinmemberChuck O'Toole23-Jun-12 8:58 
GeneralRe: A Simple C++ Question. Pinmemberjschell24-Jun-12 12:06 
AnswerRe: A Simple C++ Question. PinmemberChuck O'Toole24-Jun-12 13:29 
GeneralRe: A Simple C++ Question. Pinmemberjschell25-Jun-12 9:33 
GeneralRe: A Simple C++ Question. PinmemberAescleal23-Jun-12 7:36 
AnswerRe: A Simple C++ Question. PinmemberChuck O'Toole23-Jun-12 8:58 
GeneralRe: A Simple C++ Question. PinmemberAlbert Holguin22-Jun-12 10:05 
QuestionList of driver compilers PinmemberBrandon T. H.21-Jun-12 22:37 
AnswerRe: List of driver compilers PinmvpRichard MacCutchan22-Jun-12 1:29 
GeneralRe: List of driver compilers PinmemberBrandon T. H.22-Jun-12 17:23 
GeneralRe: List of driver compilers PinmvpRichard MacCutchan22-Jun-12 22:24 
GeneralRe: List of driver compilers PinmemberBrandon T. H.23-Jun-12 13:26 
GeneralRe: List of driver compilers PinmvpRichard MacCutchan24-Jun-12 0:16 
GeneralRe: List of driver compilers Pinmemberfat_boy22-Jun-12 23:31 
GeneralRe: List of driver compilers PinmemberBrandon T. H.23-Jun-12 14:10 
GeneralRe: List of driver compilers PinmemberBrandon T. H.23-Jun-12 14:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.141022.1 | Last Updated 23 Oct 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid