473,382 Members | 1,745 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

swapping bits between 3 chars, language C

hi friends...
i need to do this as simple as can be...

to swap the bits of 3 chars...24 bits

i must consider that the bits are in order

like
CBA
10101010 10101010 10101010
24,23,22,21,...1


in the order of the table
like:
table[24]={11,15,3,7,12,5,8,17,24,21,4,1,6,9,2,13,10,14,23, 18,20,19,22,16};
but if I change the table the swap must work....

explaining the table
the table[0] is 11, so the first bit of char A must receive the 3third bit of char B and this 3rd bit of char B must receive the first bit of A,,,a swap

24swaps...


here is my simple code...


Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2.  
  3. char getbit(unsigned char n, int posic) 
  4. {
  5.     n=n>>(posic-1); 
  6.     n= n & 0x01; 
  7.     return n;
  8. }
  9.  
  10. setbit(unsigned char *n, int posic, int valor)
  11. {
  12.      if (valor == 0)
  13.         *n = *n & ~ (1 << posic); /
  14.      else if (valor == 1)
  15.         *n = *n | (1 << posic); 
  16. }
  17.  
  18. swap(unsigned char *a, unsigned char *b, unsigned char *c)
  19. {
  20.    int i;
  21.    int table[24]={11,15,3,7,12,5,8,17,24,21,4,1,6,9,2,13,10,14,23,18,20,19,22,16};
  22.    unsigned char aux, aux2;
  23.  
  24.    for(i=0;i<24;i++)
  25.    {
  26.       if ((table[i]) <= 8) 
  27.       {
  28.  
  29.          //SOMETHING HERE
  30.       }
  31.  
  32.  
  33.       else if((table[i] >= 9) & (table[i] <= 16)) 
  34.       {
  35.  
  36.          //SOMETHING HERE
  37.       }
  38.       else(table[i] >= 17) 
  39.       {
  40.  
  41.          //SOMETHING HERE
  42.       }
  43.    }
  44. }
  45.  
  46. printbits(unsigned char n)
  47.  
  48. {
  49.  
  50.    int i;
  51.  
  52.    for(i=8;i>0;i--)
  53.  
  54.       printf("%d", getbit(n,i));         
  55.  
  56. }
  57.  
  58. main()
  59. {
  60.    unsigned char a;
  61.    unsigned char b;
  62.    unsigned char c;
  63.    a=99;
  64.    b=121;
  65.    c=144;
  66.    printbits(a);
  67.    printf("\t");
  68.    printbits(b);
  69.    printf("\t");
  70.    printbits(c);  
  71.    printf("\t\n");
  72.    swap(&a, &b, &c);
  73.    printbits(a);
  74.    printf("\t");
  75.    printbits(b);
  76.    printf("\t");
  77.    printbits(c);  
  78.    printf("\t\n\n\n");
  79.  
  80. }
  81.  
Dec 7 '07 #1
0 1277

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Zulik | last post by:
Hello, I have a problem with MessageDigest MD5 in Java. I want to calculate the digest from a file, encode it in base64 and display it. Now, according to MD5 spec, digest shall be 128 bits so,...
19
by: cppaddict | last post by:
Hi, I am going to have a series of bit flags which I could store in an array, or as a string ("10011001"), or any other way. I want to be able to turn this series of bits into an int. I know...
270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
10
by: Kristian Nybo | last post by:
Hi, I'm writing a simple image file exporter as part of a school project. To implement my image format of choice I need to work with big-endian bytes, where 'byte' of course means '8 bits', not...
11
by: Steve | last post by:
Hi, i know this is an old question (sorry) but its a different problem, i need to write a binary file as follows 00000011 00000000 00000000 00000101 00000000 11111111
3
by: Pete | last post by:
Only just started using VB and I've got a problem, not sure how to solve this one. I'm receiving a string from a comm port containing hex values which need to be converted into characters using...
4
by: Alan Ning | last post by:
Hi all, Say I have the following, string str("Hello World\n") I would like to extract 5 bits at a time from the string. I am doing this to convert ascii string to another string type. Is...
9
by: Ioannis Vranos | last post by:
Under C++03: Is it guaranteed that char, unsigned char, signed char have no padding bits?
7
by: mahdiahmadirad | last post by:
Hi dears! I wrote a simple bubble sort algorithm. it works properly when we compare full arrays but i want to sort a 2d array according to a specific part of array. it has some problem to swapping...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.