Solved

Bit Operation

Posted on 2007-04-08
21
600 Views
Last Modified: 2010-04-15
--8 bit field --
12345678

like to display it as :  21436587
0
Comment
Question by:gauravflame
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 3
  • +2
21 Comments
 
LVL 45

Assisted Solution

by:Kent Olsen
Kent Olsen earned 25 total points
ID: 18872203

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

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



Kent
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 18873082
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
 
LVL 84

Expert Comment

by:ozo
ID: 18873407
x += (x&0x55)-((x>>1)&0x55);
or
x = ((x*0x202)&0x154aa)%0x3ff;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:PaulCaswell
ID: 18873465
Oops! BIT fields, eight of them. Got it. :)

Please ignore my previous comment.

Paul
0
 

Author Comment

by:gauravflame
ID: 18880530
Can anyone explain the solution
0
 
LVL 84

Expert Comment

by:ozo
ID: 18880560
which one?
>>1 shifts
12345678 to
-1234567
&0x55 selects bits
-1-3-5-7
0
 

Author Comment

by:gauravflame
ID: 18880605
x += (x&0x55)-((x>>1)&0x55);
0
 
LVL 84

Expert Comment

by:ozo
ID: 18880687
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 Comment

by:gauravflame
ID: 18880731
How to do in programming way ?
0
 
LVL 84

Expert Comment

by:ozo
ID: 18880749
What do you mean by "do in programming way"?
0
 

Author Comment

by:gauravflame
ID: 18880788
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
 
LVL 84

Expert Comment

by:ozo
ID: 18880794
   int old = 12345678; is not an 8 bit field, it is a decimal number
which would be 101111000110000101001110 in binary
0
 
LVL 84

Expert Comment

by:ozo
ID: 18880798
Are you trying to transform the decimal number 12345678 to the decimal number 21436587?
0
 

Author Comment

by:gauravflame
ID: 18880799
yes
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 150 total points
ID: 18880837
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 18880843
btw, this is NOT a bit operation. Are you sure this is what you want to do ?
0
 

Author Comment

by:gauravflame
ID: 18885160
I thought that it can only solve through bit operations ,I mean shift and masking
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18885181
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
 
LVL 45

Expert Comment

by:Kent Olsen
ID: 18885189

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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 75 total points
ID: 18885379
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 Comment

by:gauravflame
ID: 18887811
That all make sense
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question