Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Duplicating binary data in array

Posted on 2006-10-21
5
Medium Priority
?
226 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 85

Accepted Solution

by:
ozo earned 1400 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 85

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

916 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