Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

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;
}
0
kxm
Asked:
kxm
1 Solution
 
homer99Commented:
kxm:
What errors are you getting
0
 
pjknibbsCommented:
"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
 
ozoCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
nicompCommented:
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
 
kxmAuthor Commented:
/* 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
 
kxmAuthor Commented:
/*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
 
kxmAuthor Commented:
please see my comment. thanks.
0
 
ufolk123Commented:
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
 
zhu_zhengCommented:
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
 
ufolk123Commented:
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
 
kxmAuthor Commented:
Thanks, all.
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now