Solved

Duplicating binary data in array

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Adjust Mfcapp 29 167
negation in C function 14 155
using pointers to pointers to write to a two dimensional array 16 92
Parsing XML instructions in Java/any other language.... 3 110
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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

803 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