Solved

Duplicating binary data in array

Posted on 2006-10-21
5
210 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

Technology Partners: 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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

730 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