?
Solved

c turn bit position in memory

Posted on 2010-11-19
8
Medium Priority
?
462 Views
Last Modified: 2012-05-10
Hello,
turn a bit sequence in c mingw,  unfortunately  i strand if i only try to print it out in the opposite direction, normal printout is properly

example:
11100101 11001110 11100001
should be :
10000111 01110011 10100111

 
0
Comment
Question by:thomas2010
[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
  • 4
  • 2
  • 2
8 Comments
 
LVL 16

Expert Comment

by:imladris
ID: 34177350
The example:

example:
11100101 11001110 11100001
should be :
10000111 01110011 10100111

indicates that the second line should show the same byte sequence, except each byte is printed out in reverse order.

So, in the second loop you want to be addressing the same bytes as in the first loop (that is buffer[i/8]).
In the first loop you check the top bit first (1<<(7-(i%8)) evaluates to (1<<(7-(0%8)) for i is 0, which is (1<<(7-0)), which winds up being 0x80. For i is 1, you will wind up with (1<<(7-1)), which will wind up being 0x40. So you start by checking the leftmost (Most Significant) bit, then going down to the right (to the Least Significant) bit.

In the second loop I assume you want to check the bottom bit first. So something like:

buf[i\8] & (1<<i)

would check the byte against 0x01 for i is 0, against 0x02 if i is 1, etc. etc.
0
 

Author Comment

by:thomas2010
ID: 34177570
second loop should give out each bit from right to left ( lsb to msb,)



0
 
LVL 84

Expert Comment

by:ozo
ID: 34177620
Another way could be just changing the loop to
for(int i = size*8; i>=0; i--)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 16

Expert Comment

by:imladris
ID: 34178269
>second loop should give out each bit from right to left ( lsb to msb,)

As I had surmised, and this condition for the if statement:

buf[i\8] & (1<<i)

will do exactly that. For i is 0 you will get 0x01, which is the lsb. For i is 1 you get a mask of 0x02 which is the next bit, etc.

0
 
LVL 16

Expert Comment

by:imladris
ID: 34178274
Sorry, the buf index should be division, of course, just like the first loop:

buf[i/8] & (1<<i)

0
 
LVL 16

Expert Comment

by:imladris
ID: 34189565
Did any of those answers help?

If so, it is now time to select and grade one (or more) answers.

If not, perhaps a clarifying question would help.
0
 

Author Comment

by:thomas2010
ID: 34191703
Hi . the answer not help really,
it is still not working fine. are you sure i dont need the modulus ?

void turnbits( unsigned char *buffer, int size)
{
		//OK
		for(int i = 0; i < size*8; i++)
  	{
    		if( buffer[i/8] & 1<<(7-(i%8)) )
    		{
    				printf("1");
    		}
    		else
    		{
    				printf("0");
    		}
  	}	
  	
  	printf("turn\n");
  	
  	//Not working
		for(int i = size*8; i>=0; i--)
  	{
  		if(buffer[i/8] & (1<<i) )
    		//if( buffer[((size-1)-(i/8))] & 1<<(7-(i%8)) )
    		{
    				printf("1");
    		}
    		else
    		{
    				printf("0");
    		}
  	}	
}

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 2000 total points
ID: 34193373
Either reverse the loop to i = size*8-1; i>=0; i--
or reverse the indexing to buffer[((size-1)-(i/8))] & 1<<(i%8)
not both, and not some mixture of the two
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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 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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them 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.
Suggested Courses

770 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