Solved

Duplicating binary data in array

Posted on 2006-10-21
5
215 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

734 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