468,512 Members | 1,452 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Reversal of binary int in C

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

Oct 18 '06 #1
8 4721
gsl wrote:
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

--
Nick Keighley

Oct 18 '06 #2
gsl

Nick Keighley wrote:
gsl wrote:
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

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

Oct 18 '06 #3

gsl wrote:
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?

Oct 18 '06 #4
gsl wrote:
Nick Keighley wrote:
>gsl wrote:
>>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

--
Nick Keighley

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.
Oct 18 '06 #5
"gsl" <gu***********@gmail.comwrote:
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
Oct 18 '06 #6
gsl wrote:
Nick Keighley wrote:
gsl wrote:
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

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
--
Nick Keighley

Oct 18 '06 #7
gsl wrote:
Nick Keighley wrote:
gsl wrote:
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

--
Nick Keighley

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;
}
--
Nick Keighley

Oct 18 '06 #8
gsl wrote:
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;
}
Oct 18 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

74 posts views Thread by Michael | last post: by
6 posts views Thread by ashok.anbalan | last post: by
11 posts views Thread by Tosha | last post: by
12 posts views Thread by Sathyaish | last post: by
25 posts views Thread by Frederick Gotham | last post: by
5 posts views Thread by RobertEstelle | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.