Solved

bitwise operation--exclusive

Posted on 2000-03-19
11
285 Views
Last Modified: 2013-12-14
I have an array of a structure like this:
   struct rec{
          long value;
          char binary[];
          }rec[10];
I want to have an exclusive operation on this structure. I wrote the fllowing code, but it is not working.

#include<stdio>
struct rec{
      long value;
      char binary[];// a binary char
       }rec[10];
unsigned char val;
File *fp;

main()
{
 //additional code to initialize array
   fp=foepn("file.txt","rb");
   fwrite(rec,sizeof(rec),1,fp);
   fgets(buffer,sizeof(rec),1,fp);
   val=buffer^(00000111);
   fclose(fp);
   return 0;
}
0
Comment
Question by:kxm
11 Comments
 
LVL 2

Expert Comment

by:homer99
ID: 2633822
kxm:
What errors are you getting
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2633855
"00000111" will not be interpreted as binary--it will be interpreted as octal, so it is almost certainly not the number you're expecting. As far as I know there's no way of representing a binary number directly in C code--you have to convert it to decimal, octal or hexadecimal first.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2633899
You're also trying to writing an uninitialized structure, on a FILE that was opened for reading.
the result of the xor in val is not used for anything.
you misspelled stdio.h, FILE, and fopen
fgets has the wrong number of arguments, and buffer is undefined
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Expert Comment

by:nicomp
ID: 2634554
You are reading from the end of the last thing you wrote to the file. Your code is not reading the "rec" structure that you just wrote to the file.
0
 

Author Comment

by:kxm
ID: 2634696
/* I would like to add more information to my above question, the following is the original source code. The purpose is to read an integer number, then read another number, and then perform bitwise exclusive operation among those two numbers */


#include<stdio.h>

struct record{
      int value;
      long num;
      }rec1;

int number, temp;
char *key, *t,*exclusive;

char  *itobs(int, char*);

main()
{
  char bin_str[8*sizeof(int)+1];
  temp=2;
  puts("Enter integers and see them  in   binary.");
  puts("Non-numberic input terminate program.");

  while(scanf("%d",&number)==1)
 {
/*******input the key**************/
key=itobs(number,bin_str);

/********input the temp******/
puts("Enter temp value:");
scanf("%d",&temp);
t=itobs(temp,bin_str);

/*****exclusive temp and key******/
*t=*t^*key;
//t=itobs(temp,bin_str);
printf("exclusive value %s.\n", t);

 }
  return 0;
}

char *itobs(int n, char *ps)
{
 int i;
 static int size=8*sizeof(int);

 for(i=size-1;i>=0;i--, n>>=1)
 ps[i]=(01&n)+'0';
 ps[size]='\0';
 return ps;
}

The returned value is 9206 no matter what input numbers are. Please advise me what is wrong in the code.

Thanks, --kxm
0
 

Author Comment

by:kxm
ID: 2634751
/*more information. This modified source code is try to perform exclusive operation to structure rec1. The ultimate goal is to encrypt this rec1 via an encryption key and save it to a file*/


#include<stdio.h>

struct record{
            int value;
            long num;
            }rec1={
                        3,
                        2
                        };

int number, temp;
char *key, *t,*exclusive,k;

char  *itobs(int, char*);

main()
{
      char bin_str[8*sizeof(int)+1];
      puts("Enter integers number.");
      puts("Non-numberic input terminate program.");

      while(scanf("%d",&number)==1)
      {
      /********input the temp******/
            puts("Enter temp value:");
            scanf("%d",&temp);

            /*****exclusive temp and key******/
            //*t=8^12;
            //*t=*t^*key;
            //t=itobs(temp,bin_str);
            k=rec1^number;
            //printf("exclusive value %u.\n", *t);
            printf("exclusive value %u.\n",k);
      }
      return 0;
}


/* function to convert an integer to the binary format */

char *itobs(int n, char *ps)
{
      int i;
      static int size=8*sizeof(int);

      for(i=size-1;i>=0;i--, n>>=1)
            ps[i]=(01&n)+'0';
      ps[size]='\0';
      return ps;
}
0
 

Author Comment

by:kxm
ID: 2634753
please see my comment. thanks.
0
 
LVL 3

Accepted Solution

by:
ufolk123 earned 25 total points
ID: 2634949
Hi kxm,


This coding may have problems.

/*****exclusive temp and key******/
*t=*t^*key;

As far as i understand you want to do the XOR of these binary represented strings ,but what is happening here is that this operaiton will only XOR the first characters of both string .Alternatively you should code like this.

/** Insert this block ***/
{
int xnum;
xnum=number^temp; //calculate XOR
t=itobs(xnum,bin_str);
}

0
 

Expert Comment

by:zhu_zheng
ID: 2637041
Hi,

Thank you all for the answers.

For the proposed answer, thank you, ufolk123. You have answered 80% of  my question. As for your explanation, exclusive can not performed on two binary strings. If I want to do so, I have to convert the binary string to decimal number, do the exclusive on the decimal number. If I really want the binary string output, I have to convert the decimal to binary. Am I understand right?

Also, I add more comments on this question on 3/19/00, 7:27pm PST. my ultimate goal is to encrypt a struct array. Please see my comment for the code. Can I do the exclusive on the whole structure, or I have to perform exclusive on each argument of structure?

Please advise.

Thanks a lot,--kxm
0
 
LVL 3

Expert Comment

by:ufolk123
ID: 2638633
Dear Zhu-zheng,

/*******
As for your explanation, exclusive can not performed on two binary strings. If I want to do so, I have to convert the binary string to decimal number, do the exclusive on the decimal number. If I really want the binary string output, I have to convert the decimal to binary. Am I understand right?
**********/
Actually everthing inside system is as you know is binary only but the problem here is all numbers has binary data value so any bit operations can be done on them .When you convert them into binary strings actually that string is only have a binary presentation.Internally it is stored as
e.g string s1="1000";
memory store:ASCII1 ASCII0 ASCII0 ASCII0
where ASCII1 <> 1 (value)
ASCII0 <> 0
So the real data value of string is drastically differnt from the datum it stores i.e binary ( 1000 =8 decimal).
So for presentation sake you can change any number but always do all type of numeric operation always on the actual data on the representation.

/*************
Also, I add more comments on this question on 3/19/00, 7:27pm PST. my ultimate goal is to encrypt a struct array. Please see my comment for the code. Can I do the exclusive on the whole structure, or I have to perform exclusive on each argument of structure?
************/

Yeh I have see your code,Actually there is no generic method to crypt an array .You can have two approaches here.
1) crypt each field one by one
advantages: your code will be understandable
It will be portable
but this is subjected to conditions that all fields in the struct are numeric ( real numbers).
2)treat struct  as array of characters ( or intergers if your key is integer) and crypt each such integer with key.The number of integers you will get is
sizeof(strcut)/sizeof(int)
problems here will be
1)struct may not be exact multiple of integer ( there is no problem if your key is character).
2)Your code may not be portable as strcut may be padded inside by spaces ( inserted by compiler for optimisation) and these paddings may vary from compiler to compiler ( some control is possible using pragma pack).

So if you want to do it fast and tricky can do by treating struct data as array of your keysize.Problem here is portability.

Other is you crypt individual fields problem here if your strcut has non-numeirc data it will not work.

0
 

Author Comment

by:kxm
ID: 2643064
Thanks, all.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question