• C

Bit Operation

--8 bit field --
12345678

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

Improve company productivity with a Business Account.Sign Up

x
 
Infinity08Connect With a Mentor 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
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:

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
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
ozoCommented:
x += (x&0x55)-((x>>1)&0x55);
or
x = ((x*0x202)&0x154aa)%0x3ff;
0
 
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:
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 OlsenData Warehouse Architect / 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
 
ozoConnect With a Mentor 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
 
gauravflameAuthor Commented:
That all make sense
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.