2 Replies - 365 Views - Last Post: 14 March 2010 - 08:22 AM Rate Topic: -----

#1 vash4884  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 17-April 07

Bitshifting help

Posted 13 March 2010 - 01:57 PM

Hello I'm working on an assignment for my C class and I've got it pretty much finished except for this last function which is not working properly. What this function does is takes a string of 4 characters and a string of 28 numbers to manipulate the first 7 bits of each string. so if i is at 0 and gI is 25 it will move bit 0 from the first character and move it to the 25th spot which is in the new 4th character if that makes sense.

old char1 0-6 bits
new char1 0-6 bits
I hope this makes sense what I'm trying to do. Here's my function i'm trying to make but it's not printing out the right thing.

if you guys can help it'll really really help me out :)
int encryption(int fI[], char cutCipher[]) {
	printf(" \nin encryption blah \n");
	unsigned char oldChar[4] = { cutCipher[0], cutCipher[1], cutCipher[2],
		cutCipher[3] };
	unsigned char newChar[4] = {0,0,0,0};
	unsigned char newA = 0;
	unsigned char newB = 0;
	unsigned char newC = 0;
	unsigned char newD = 0;
	unsigned char m = 1;
	unsigned char m2 = 0;
	int i = 0;
	int place = 0;
	//printf("a is %c", a);
	for (i = 0; i < 28; i++) {
		if ((0 <= i) &&(i<= 6)) {
			place = 0;
		}
		if ((7<= i) &&(i<= 13)) {
			place = 1;
		}
		if ((14 <= i) &&(i<= 20)) {
			place = 2;
		}
		if ((21 <= i) &&(i<= 27)) {
			place = 3;
		}
		if (i == 0 || i == 7 || i == 14 || i == 20) {
					m = oldChar[place] & (m );

				}
		if (i == 1 || i == 8 || i == 15 || i == 21) {
			m = oldChar[place] & (m << 1);
			m = m >> 1;
		}
		if (i == 2 || i == 9 || i == 16 || i == 22) {
			m = oldChar[place] & (m << 2);
			m = m >> 2;
		}
		if (i == 3 || i == 10 || i == 17 || i == 23) {
			m = oldChar[place] & (m << 3);
			m = m >> 3;
		}
		if (i == 4 || i == 11 || i == 18 || i == 24) {
			m = oldChar[place] & (m << 4);
			m = m >> 4;
		}
		if (i == 5 || i == 12 || i == 19 || i == 25) {
			m = oldChar[place] & (m << 5);
			m = m >> 5;
		}
		if (i == 6 || i == 13 || i == 20 || i == 26) {
			m = oldChar[place] & (m << 6);
			m = m >> 6;
		}

		/* this is to put the bins in the appropriate bits in the new array */
		if ((0 <= fI[i])&& (fI[i] <= 6)) {
			place = 0;
		}
		if ((7 <= fI[i])&& (fI[i] <= 13)) {
			place = 1;
		}
		if ((14 <= fI[i])&& (fI[i] <= 20)) {
			place = 2;
		}
		if ((21 <= fI[i])&& (fI[i] <= 27)) {
			place = 3;
		}
		if (fI[i] == 0 || fI[i] == 7 || fI[i] == 14 || fI[i] == 21) {
					newChar[place] += m ;

				}

		if (fI[i] == 1 || fI[i] == 8 || fI[i] == 15 || fI[i] == 22) {
			newChar[place] += (m << 1);

		}
		if (fI[i] == 2 || fI[i] == 9 || fI[i] == 16 || fI[i] == 23) {
			newChar[place] += (m << 2);
		}
		if (fI[i] == 3 || fI[i] == 10 || fI[i] == 17 || fI[i] == 24) {
			newChar[place] += (m << 3);
		}
		if (fI[i] == 4 || fI[i] == 11 || fI[i] == 18 || fI[i] == 25) {
			newChar[place] += (m << 4);
		}
		if (fI[i] == 5 || fI[i] == 12 || fI[i] == 19 || fI[i] == 26) {
			newChar[place] += (m << 5);
		}
		if (fI[i] == 6 || fI[i] == 13 || fI[i] == 20 || fI[i] == 27) {
			newChar[place] += (m << 6);
		}

		m = 1;
	}
	printf("\nOld is ");
	for (i = 0; i<4; i++) {
		printf("%c",oldChar[i]);
	}
	printf("\nNew is %s\n",newChar);
	for (i = 0; i<4; i++) {
			printf("%c",newChar[i]);
		}
	return OK;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Bitshifting help

#2 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Bitshifting help

Posted 14 March 2010 - 08:01 AM

By the "first 7 bits" do you mean the high order first seven or low order?

Assuming an 8 bit byte:
One Byte: 11110000
High Order Low Order
[1111] [0000]

so you mean bits 1111 and 000 ?
I'm an idiot, and it's hard to see what you're trying to do..
Was This Post Helpful? 0
  • +
  • -

#3 horace  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 290
  • View blog
  • Posts: 1,898
  • Joined: 25-October 06

Re: Bitshifting help

Posted 14 March 2010 - 08:22 AM

you have a very complex algorithm and I cannot figure out your logic

would a simpler approach be to extact the 28 bits of the characters into an array, then swop the values in the array as specified by table fI[], then put the 28 bits into 4 new encoded characters

e.g. extracting the 28 bits
int encryption(int fI[28], char cutCipher[4]) {
    char bits[28]={0}, newBits[28]={0};
    int i;
    // extract 28 bits from the characters (7 bits/char)
    for(i=0;i<28;i++)
       {
        if(i<7)  bits[i]=(cutCipher[0]>>i) & 1;
        else
        if(i<14) bits[i]=(cutCipher[1]>>(i-7)) & 1;
       else
        if(i<21) bits[i]=(cutCipher[2]>>(i-14)) & 1;
       else
         bits[i]=(cutCipher[3]>>(i-21)) & 1;
       printf("%d ", bits[i]);

       }
  ... now copy the bit values from bits[] into newBits[] as per table fI[]
  ... create 4 characters from newBits[]


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1