Thread: Misadventures with bitwise operators

  1. #1
    Widdle Coding Peon Aerie's Avatar
    Join Date
    Dec 2004
    Posts
    115

    Misadventures with bitwise operators

    I'm trying to make a program that solves exercise 2-6 in K&R: "Write a function setbits(x,p,n,y) that returns x with the n bits that begin at position p set to the rightmost n bits of y, leaving the other bits unchanged."

    While I renamed/reordered the function and variables, so I wouldn't get mixed up, this code should(in theory) do the same thing.

    Code:
    #include <stdio.h>
    
    unsigned int xfrbits(unsigned int origvar, unsigned int destvar, int xfrnum, int xfradr)
    {
    unsigned int tmpmask;
    tmpmask = ~(~0U << xfrnum)
    origvar &= tmpmask;
    tmpmask = ~(tmpmask << xfradr);
    destvar &= tmpmask;
    origvar <<= xfradr;
    destvar ^= origvar;
    return destvar;
    }
    
    int main(void)
    {
    unsigned int origvar, destvar, spchldr;
    int xfrnum, xfradr;
    for(origvar = 0; origvar <= 250; origvar += 49)
    	for(destvar = 0; destvar <= 250; destvar += 49)
    		for(xfrnum = 0; xfrnum <= 8; xfrnum++)
    			for(xfradr = 0; xfradr <= 8; xfradr++){
    				spchldr = xfrbits(origvar, destvar, xfrnum, xfradr);
    				printf("%u, %u, %d, %d : %u\n", origvar, destvar, xfrnum, xfradr, spchldr));
    					}
    return 0;
    }
    I understand the principle here(I think), but I want to make sure I'm not getting mixed up.

    Here's the logic I used to solve the exercise:

    1. Generate bitmask to erase all the bits in origvar not being transfered, apply this mask to origvar.

    2. Alter this bitmask to erase the bits to be overwritten by origvar, apply this mask to destvar.

    3. Bitshift origvar to place the bits correctly.

    4. Apply origvar to destvar to transfer the bits.

    Please, be nice if you find problems with this - I mean, tell me exactly what I did wrong, but keep in mind that I had some trouble focusing long enough to write it.

    Note: The extra assignment of
    Code:
    spchldr = xfrbits(...);
    is just to save screen space for this post; I'd normally call xfrbits inside the printf that follows.
    I live in a giant bucket.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your solution is just a tad overkill. Try to use getbits as your example, and remember that reuse is a good thing. You'll find that the solution really isn't that much longer (and is simpler) than the one for getbits.

    >Please, be nice if you find problems with this
    Well, it's not correct in that it doesn't do what the exercise asks for. You can test the bit result with something quick and dirty like this:
    Code:
    void showbits(unsigned int x)
    {
      int i;
    
      for (i = 15; i >= 0; i--)
        printf("%d", !!(x & (1U << i)));
      printf("\n");
    }
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bitwise operators
    By gnewfenix in forum C Programming
    Replies: 2
    Last Post: 05-16-2009, 08:43 PM
  2. Bitwise Operators
    By rrc55 in forum C Programming
    Replies: 6
    Last Post: 04-30-2009, 11:37 AM
  3. Palindromes and Bitwise operators
    By Dr Tornillo in forum C Programming
    Replies: 8
    Last Post: 08-02-2007, 02:31 PM
  4. bitwise and arithmetic Operators
    By Whiteghost in forum C Programming
    Replies: 4
    Last Post: 12-28-2006, 02:13 PM
  5. Bitwise Operators, Help!!
    By Mini__C in forum C Programming
    Replies: 6
    Last Post: 07-14-2004, 04:20 PM