Reversal of binary int in C

G

gsl

Can anyone tell me how to reverse an integer number represented in
binary form(or we can say binary reversal of int)?
for ex: given 101100 .... the answer should be 001101
 
N

Nick Keighley

gsl said:
Can anyone tell me how to reverse an integer number represented in
binary form(or we can say binary reversal of int)?
for ex: given 101100 .... the answer should be 001101

loop from low-bit to hi-bit
print-bit
end loop
 
G

gsl

Nick said:
loop from low-bit to hi-bit
print-bit
end loop

Hey i dont want to print the reversal of string.... i want to store it
in the same location in the reverse order
 
M

mark_bluemel

gsl said:
Can anyone tell me how to reverse an integer number represented in
binary form(or we can say binary reversal of int)?
for ex: given 101100 .... the answer should be 001101

Why? What are you actually trying to achieve? (homework?)

What have you already tried?

If you want help, fine, but show us that you're prepared to do some
work yourself.

Have you STFW'ed (Searched The Fine Web), scanned Google Groups for
this newsgroup (this sort of question comes up fairly often - I found a
complete answer to your question dated 2002), checked out the FAQ?
 
?

=?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=

gsl said:
Hey i dont want to print the reversal of string.... i want to store it
in the same location in the reverse order
If you can get bit N from integer M with
M & 1<<N
you should hopefully be able to store that at the proper "reversed"
place using a loop and a temporary integer to store the reverse value.
 
R

Richard Bos

gsl said:
Can anyone tell me how to reverse an integer number represented in
binary form(or we can say binary reversal of int)?
for ex: given 101100 .... the answer should be 001101

Simple. Print it to a string - reverse the string - read it back from
the string. Solutions to the first two parts, especially the second,
have been posted, discussed, requested and debugged in this very group,
over the last years, /passim/; so just search any available Usenet
archive. The last part is just a matter of strtoul().

Richard
 
N

Nick Keighley

gsl said:
Hey i dont want to print the reversal of string.... i want to store it
in the same location in the reverse order

loop from low-bit to hi-bit
store-bit
end loop
 
N

Nick Keighley

gsl said:
Hey i dont want to print the reversal of string.... i want to store it
in the same location in the reverse order

hope this helps:-

#include <assert.h>
#include <stdlib.h> /* abort() */
#include <stdio.h>

void bit_reverse (unsigned* word)
{
// write code here
*word = 1;
}

void test_br (unsigned input, unsigned expected)
{
unsigned result = input;

bit_reverse (&result);

if (result != expected)
{
fprintf (stderr, "TEST FAILED: input %x expected %x result
%x\n",
input, expected, result);
abort();
}
}

void test (void)
{
test_br (0x2c, 0x0d);
}

int main (void)
{
test();
return 0;
}
 
J

jmcgill

gsl said:
Can anyone tell me how to reverse an integer number represented in
binary form(or we can say binary reversal of int)?
for ex: given 101100 .... the answer should be 001101

Your homework question is pretty silly; it would be more generally
useful to implement bitwise rotates, or maybe matrix transpose, than
this reversal. Nonetheless I think this works.


#include <stdio.h>
#include <stdlib.h>

void
printb(int d)
{
int sz = 8 * sizeof d;
int i;
for (i = sz - 1; i >= 0; i--) {
printf("%s", (d & (1 << i)) ? "1" : "0");
}
}

int
rev(int src)
{
int i;
int retval = 0;
int sz = 8 * sizeof src;
for (i = 0; i < sz; i++) {
retval |= (((src) & 1) << (sz - i - 1));
src >>= 1;
}
return retval;
}

int
main(int argc, char **argv)
{
int d;
d = atoi(argv[1]);
printb(d);
printf(", ");
printb(rev(d));
printf("\n");
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,434
Messages
2,571,689
Members
48,796
Latest member
Greg L.

Latest Threads

Top