Thread: Please help me in Binary XOR operation using C++?

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Please help me in Binary XOR operation using C++?

    Please help me in Binary XOR operation.... This program will ask user to enter their input..After that the program will convert to Binary Code for each character that user have entered. After that i create a multidimensional array to store for binary of each character...so from an array, i want to make a XOR operation for each binary..

    Example :-
    User Input : syahril
    Binary for each character is :-
    s = 01110011
    y = 01111001
    a = 01100001
    h = 01101000
    r = 01110010
    i = 01101001
    l = 01101100

    So, my array will be like this

    0 0 0 0 0 0 0
    1 1 1 1 1 1 1
    1 1 1 1 1 1 1
    1 1 0 0 1 0 0
    0 1 0 1 0 1 1
    0 0 0 0 0 0 1
    1 0 0 0 1 0 0

    ** the top row is the left most side for each character
    ** the bottom row is the right most side for each character

    so...my program will do XOR operation for each binary in array start with top row from left to right...
    so...when i executed and run my program it give to me an output like this = 00000000
    but when i do a manual calculation for XOR operation based on the example, i will get an output like this = 01110100...

    Please help me...i didnt know which part that im wrong or missing....


    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <iostream.h>
    
    char *Entry, Letter, Choice[2];
    int Ascii, len, Binary[8], Total;
    void Convert();
    int f = 0;
    int Special[8][100];
    int temp[100];
    int hold[100];
    int k =0;
    int s=2;
    int z=1;
    int h=2;
    int m=1;
    int x=0;
    int u=0;
    int y = 0;
    int result[8];
    int la = 0;
    int temp_len;
    
    
    void main()
    {
    Convert();
    }
    
    void Convert()
    {
    Entry = new char[501];
    cout << "Enter Text To Convert(Up To 500 Chars): ";
    cin.getline(Entry, 500);
    len = strlen(Entry);
    temp_len = len;
    cout<<"\n\n";
    
    for(int d = 0; d < len; d++)
    {
    Total = 7;
    Letter = Entry[d];
    Ascii = Letter;
    while(Ascii > 0)
    {
    if((Ascii%2)==0)
    {
    Binary[Total] = 0;
    Ascii = Ascii/2;
    Total--;
    }
    else
    {
    Binary[Total] = 1;
    Ascii = Ascii/2;
    Total--;
    }
    }
    
    for(int e = 0; e < 8; e++)
    {
    cout << Binary[e];
    Special[e][f] = Binary[e];
    }
    f++;
    
    cout<<"\n\n";
    
    }
    
    cout<<"\n\nThe output Display in Array : \n\n";
    
    
    for(int l=0;l<8;l++)
    {
    hold[y]=Special[l][y];
    
    if(hold[y]==Special[l][k++])
    {
    temp[z]=0;
    while(s<len)
    {
    if(temp[z]==Special[l][h])
    {
    temp[m++]=0;
    h++;
    }
    else
    {
    temp[m++]=1;
    h++;
    }
    s++;
    z++;
    }
    }
    else
    {
    temp[z]=1;
    
    while(s<len)
    {
    if(temp[z]==Special[l][h])
    {
    temp[m++]=0;
    h++;
    }
    else
    {
    temp[m++]=1;
    h++;
    }
    s++;
    z++;
    }
    
    }
    
    result[u] = temp[temp_len--];
    u++;
    
    k=0;
    s=2;
    z=1;
    h=2;
    m=1;
    y=0;
    temp_len = len;
    
    }
    
    while(la<8)
    {
    for(int oop = 0;oop <len;oop++)
    {
    cout<<Special[la][oop]<<" ";
    }
    la++;
    cout<<"\n\n";
    }
    
    cout<<"\n\nThe result for XOR operation is : \n";
    for(int t=0;t<8;t++)
    {
    cout<<result[t];
    }
    
    cout<<"\n\n";
    
    delete[] Entry; 
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  2. Replies: 0
    Last Post: 11-04-2006, 11:07 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM