• C

# Bit Operation

--8 bit field --
12345678

like to display it as :  21436587
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DBACommented:

If this is really a bit operation, just mask and shift;

new = ((old & 0x55) << 1) | ((old >> 1)  & 0x55);

Kent
0
Commented:
This is the standard endian conversion for 32 bit values in a 16 bit environment.

unsigned long chengeEndian(unsigned long n) {
unsigned char * b = (unsigned char *)&n;
unsigned long x = (b[3]<<24) +(b[2]<<16)+(b[[1]<<8)+b[0];
return x;
}

Paul
0
Commented:
x += (x&0x55)-((x>>1)&0x55);
or
x = ((x*0x202)&0x154aa)%0x3ff;
0
Commented:
Oops! BIT fields, eight of them. Got it. :)

Paul
0
Author Commented:
Can anyone explain the solution
0
Commented:
which one?
>>1 shifts
12345678 to
-1234567
&0x55 selects bits
-1-3-5-7
0
Author Commented:
x += (x&0x55)-((x>>1)&0x55);
0
Commented:
1·3·5·7·
-
·1·3·5·7
=
·1·3·5·7

·2·4·6·8
+
·2·4·6·8
=
2·4·6·8·
0
Author Commented:
How to do in programming way ?
0
Commented:
What do you mean by "do in programming way"?
0
Author Commented:
Something in this way ..needs correction

int main()
{

int old = 12345678;
int new = ((old*0x202)&0x154aa)%0x3ff;
printf("\n %d \n",new);

return 0;

}
0
Commented:
int old = 12345678; is not an 8 bit field, it is a decimal number
which would be 101111000110000101001110 in binary
0
Commented:
Are you trying to transform the decimal number 12345678 to the decimal number 21436587?
0
Author Commented:
yes
0
Commented:
>> Are you trying to transform the decimal number 12345678 to the decimal number 21436587?

That makes things completely different heh.

You could do something like this :

#include <stdio.h>

void swap(char *c1, char *c2) {
char tmp = *c1;
*c1 = *c2;
*c2 = tmp;
return;
}

int reorder(int oldd) {
int newd = 0;
char buf[17] = { 0 };
int i = 0;
sprintf(buf, "%016d", oldd);
for (i = 0; i < 16; i += 2) {
swap(&buf[i], &buf[i + 1]);
}
sscanf(buf, "%d", &newd);
return newd;
}

int main(void) {
int oldd = 12345678;
int newd = reorder(oldd);
printf("\n %d \n",newd);
return 0;
}
0

Experts Exchange Solution brought to you by

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Commented:
btw, this is NOT a bit operation. Are you sure this is what you want to do ?
0
Author Commented:
I thought that it can only solve through bit operations ,I mean shift and masking
0
Commented:
shifting and masking are essentially bit operations. Although they can be extrapolated to byte level. In this case, it wouldn't make things easier.
0
DBACommented:

Your problem is arithmetic, not logical.  That is, it's a base 10 arithmetic problem, not a base 2 problem.

As Infinity shows, converting the value to ASCII (so that you can read it) and swapping the bytes does what you want.   :)

Kent
0
Commented:
you could  use bit operations to implement arithmetic operations, but it's tedious when C already had built in arithmetic operations.
You can swap a pair of digits with
oldd=12;
newd = oldd*1000 % 9999 % 990;
you can also use bit operations to change
0x12345678 to 0x21436587
0
Author Commented:
That all make sense
0
###### It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.