Thread: Bit transposing

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Bit transposing

    Hello,
    i was wondering on how to do a bit transposition.
    I really need this for a project i am doing encrypting a data base.
    and i am stumped on this one.
    all help is appreciated
    thanks

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    You can determine whether or not a particular bit is turned on by using the & operator, ie:
    Code:
    if (value & 0x01)
    if (value & 0x02)
    if (value & 0x04)
    
    if (value & pow(2,0))
    if (value & pow(2,1))
    if (value & pow(2,2))
    To ensure a bit is turned on, do this:
    Code:
    value|=0x01;
    To ensure it is off, do this:
    Code:
    value&=~(0x01);
    Note that those last two code pieces will ensure the bit state no matter what it was orginally, so they're safe to use without first checking the state.

    I suggest a function signature like this:
    Code:
    int SwapBits(int value, int index1,int index2);
    Good luck. Have a shot, then post any problems you have.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-10-2005, 10:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM