• C

Bit Operation

--8 bit field --
12345678

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

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Kent OlsenDBACommented:

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

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



Kent
0
PaulCaswellCommented:
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
ozoCommented:
x += (x&0x55)-((x>>1)&0x55);
or
x = ((x*0x202)&0x154aa)%0x3ff;
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

PaulCaswellCommented:
Oops! BIT fields, eight of them. Got it. :)

Please ignore my previous comment.

Paul
0
gauravflameAuthor Commented:
Can anyone explain the solution
0
ozoCommented:
which one?
>>1 shifts
12345678 to
-1234567
&0x55 selects bits
-1-3-5-7
0
gauravflameAuthor Commented:
x += (x&0x55)-((x>>1)&0x55);
0
ozoCommented:
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
gauravflameAuthor Commented:
How to do in programming way ?
0
ozoCommented:
What do you mean by "do in programming way"?
0
gauravflameAuthor 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
ozoCommented:
   int old = 12345678; is not an 8 bit field, it is a decimal number
which would be 101111000110000101001110 in binary
0
ozoCommented:
Are you trying to transform the decimal number 12345678 to the decimal number 21436587?
0
gauravflameAuthor Commented:
yes
0
Infinity08Commented:
>> 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

Your issues matter to us.

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

Start your 7-day free trial
Infinity08Commented:
btw, this is NOT a bit operation. Are you sure this is what you want to do ?
0
gauravflameAuthor Commented:
I thought that it can only solve through bit operations ,I mean shift and masking
0
Infinity08Commented:
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
Kent OlsenDBACommented:

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
ozoCommented:
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
gauravflameAuthor 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.