Thread: Duplicating specific binary data in array

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    1

    Duplicating specific binary data in array

    Hi,
    I have an array of unsigned longs called "lookup". I need to copy a bit from one part of the "marix" to another.
    i.e. at the jth bit from the right in row[i] should equal the xth bit from the right in row[y].

    I'm trying to do this with bitwise operators. I've come up with somthing like:

    Code:
    lookup[i] |= (lookup[y] & (1L << x)) >> (j-31)
    (Don't worry, there arn't any magic numbers in the actual program!)
    For instance, when i = 2, j=29, y=1, x=30:
    Start:
    Code:
      lookup[0] = 0 0 0 0 0 0 0 0 0 ... 0
      lookup[1] = 0 1 0 0 0 0 0 0 0 ... 0
      lookup[2] = 0 0 0 0 0 0 0 0 0 ... 0
    Should finish up like:
    Code:
      lookup[0] = 0 0 0 0 0 0 0 0 0 ... 0
      lookup[1] = 0 1 0 0 0 0 0 0 0 ... 0
      lookup[2] = 0 0 1 0 0 0 0 0 0 ... 0
    Any help would be really appreciated.

    Chris

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What actually happens? Are you using signed numbers?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Dynamic Array substitution in composite data variable
    By DavidDobson in forum C Programming
    Replies: 2
    Last Post: 08-12-2008, 04:29 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  5. C diamonds and perls :°)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 10:19 PM