Solved

Bit Operation

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

like to display it as :  21436587
0
Comment
Question by:gauravflame
  • 7
  • 7
  • 3
  • +2
21 Comments
 
LVL 45

Assisted Solution

by:Kdo
Kdo 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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:Kdo
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

758 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now