Solved

Duplicating binary data in array

Posted on 2006-10-21
5
178 Views
Last Modified: 2010-04-15
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:

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:
  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:
  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
0
Comment
Question by:design_404
  • 2
  • 2
5 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 350 total points
ID: 17782619
lookup[i] |= ((lookup[y]>>x) & 1) << j;
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17782623
> at the jth bit from the right in row[i] should equal the xth bit from the right in row[y].

row[i] = row[i] & (~(1<<j));     //clear off the target bit

sourcebit = !(!(row[y] & (1<<x))); //get if the bit at source is 0 or 1

row[i] = row[i] | (sourcebit<<j);   //set the bit

Ofcourse it is possible to shrink all of this in one line, but it is much more readable this way.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 17782625
Problem with not clearing target bit before ORing is that if previous value of this bit was 1 and new value was 0, results will be incorrect.
0
 

Author Comment

by:design_404
ID: 17782643
Thanks everybody! Ozo got it first with the best answer so he gets the points. (Sorry sunnycoder! I don't check my email often enough). Thanks heaps.
0
 
LVL 84

Expert Comment

by:ozo
ID: 17782647
lookup[i] ^= (((lookup[y]>>x ^ lookup[i]>>j) & 1) << j);
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

747 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

13 Experts available now in Live!

Get 1:1 Help Now