Link to home
Start Free TrialLog in
Avatar of kxm
kxm

asked on

bitwise operation--exclusive

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;
}
Avatar of homer99
homer99

kxm:
What errors are you getting
"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.
Avatar of ozo
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
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.
Avatar of kxm

ASKER

/* 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
Avatar of kxm

ASKER

/*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;
}
Avatar of kxm

ASKER

please see my comment. thanks.
ASKER CERTIFIED SOLUTION
Avatar of ufolk123
ufolk123

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.

Avatar of kxm

ASKER

Thanks, all.