473,799 Members | 3,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

reverse the bits in an interger?

KG
Could any one tell me how to reverse the bits in an interger?

Jun 20 '07 #1
40 36495
KG said:
Could any one tell me how to reverse the bits in an interger?
int reverse_bits(in t n)
{
return ~n;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #2
"Richard Heathfield" <rj*@see.sig.in validschrieb im Newsbeitrag
news:Jb******** *************** *******@bt.com. ..
KG said:
>Could any one tell me how to reverse the bits in an interger?

int reverse_bits(in t n)
{
return ~n;
}
That's inverting (1 turn into 0 and vice versa), not reverting. I understood
the OP wants the laest significant bit being the most significate, the 2nd
least being the second most and so on.

Bye, Jojo
Jun 20 '07 #3
KG <Kr************ **@gmail.comwri tes:
Could any one tell me how to reverse the bits in an interger?
Use a loop. For instance here you have a pseudocode (foo[i] represents
ith bit):

#v+
output := 0;
FOR i := 0 TO number of bits:
IF input[i] is set
set output[number of bits - i];
#v-

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl >---<jid:mina86*chr ome.pl>--ooO--(_)--Ooo--
Jun 20 '07 #4
On Jun 20, 2:40 pm, KG <Kriti.Goyal.t. ..@gmail.comwro te:
Could any one tell me how to reverse the bits in an interger?
First of all you need to know the size of integer, then you should go
somthing like this:

<snip>
int cnt=0,num=0,tem p=0,res=0;
while(cnt<32) // here i consider size of integer to be 32-bit
{
temp=num & 1; // num contains the value of integer
res=res | temp;
num=0;
num=num>>1;
res=res<<1;
cnt++;
}
</snip>

I havent checked it. But still the idea will like this only. Also
check for the 32 iteration.
Thanks and Regards,
ar
--
"Let's plan a murder, or start a new religion."
Jun 20 '07 #5
Michal Nazarewicz wrote:
KG <Kr************ **@gmail.comwri tes:
>Could any one tell me how to reverse the bits in an interger?

Use a loop. For instance here you have a pseudocode (foo[i] represents
ith bit):

#v+
output := 0;
FOR i := 0 TO number of bits:
Suppose we have 2-bit integers, so number of bits is 2; and assuming
the usual reading of a FOR-TO loop ...
IF input[i] is set
.... there will be an array indexing problem, since `i` will take on
the values 0, 1, 2 and either the index `0` or `2` will be wrong.
I suspect you want `number of bits - 1`. Or even better

for i from lowestBitIndex to highestBitIndex do

or, since the ordering doesn't matter

for i in validIndexes do

where `validIndexes` is the set of legal index values.

Encoding these is C is of course straightforward .

--
Chris "unless I play a Rune card" Dollin

Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

Jun 20 '07 #6
KG wrote:
Could any one tell me how to reverse the bits in an interger?
---------snip------------
#include <string.h>
#include <stdio.h>

unsigned int revbits(unsigne d int n)
{
unsigned int i, result = 0;

for (i = sizeof(int) * 8; i; --i)
{
result <<= 1;
if (n & 1)
result |= 1;
n >>= 1;
}
return result;
}

int main(void)
{
int i = (sizeof(int) * 8);

while (--i >= 0)
printf("IN:0x%. 08x OUT:0x%.08x\n",
1<<i, revbits(1<<i));
return 0;
};
-----------snap----------
tk@localhost ~/test $ gcc -O2 -Wall -o test test.c
tk@localhost ~/test $ ./test
IN:0x80000000 OUT:0x00000001
IN:0x40000000 OUT:0x00000002
IN:0x20000000 OUT:0x00000004
IN:0x10000000 OUT:0x00000008
..............
IN:0x00000004 OUT:0x20000000
IN:0x00000002 OUT:0x40000000
IN:0x00000001 OUT:0x80000000

(the printf doesnt take sizeof(int) into account)
Jun 20 '07 #7
Joachim Schmitz said:
"Richard Heathfield" <rj*@see.sig.in validschrieb im Newsbeitrag
news:Jb******** *************** *******@bt.com. ..
>KG said:
>>Could any one tell me how to reverse the bits in an interger?

int reverse_bits(in t n)
{
return ~n;
}
That's inverting (1 turn into 0 and vice versa), not reverting.
It reverses the sense of each bit.
I understood the OP wants the laest significant bit being the most
significate, the 2nd least being the second most and so on.
And so you have (unwittingly?) made explicit the implicit lesson that
specifications *matter*.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 20 '07 #8

"Torsten Karwoth" <ag******@gmx.d eha scritto nel messaggio news:5d******** *****@mid.uni-berlin.de...
KG wrote:
>Could any one tell me how to reverse the bits in an interger?

---------snip------------
#include <string.h>
#include <stdio.h>

unsigned int revbits(unsigne d int n)
{
unsigned int i, result = 0;

for (i = sizeof(int) * 8; i; --i)
#include <limits.hand use CHAR_BIT instead of 8.
Also, what if there are padding bits?
To be *totally* portable: (yes, most machines have 8-bit bytes and
no padding in unsigned int's, but that's not required by the C
standard)
#include <limits.h>

int count_bits(unsi gned int n)
{
int result = 1;
while (n /= 2)
result++;
return result;
}

unsigned int revbits(unsigne d int n)
{
unsigned int i, result = 0;
for (i = count_bits(UINT _MAX); i; --i)
{
result <<= 1;
if (n & 1)
result |= 1;
n >>= 1;
}
return result;
}

int main(void)
{
int i = (sizeof(int) * 8);

while (--i >= 0)
printf("IN:0x%. 08x OUT:0x%.08x\n",
1<<i, revbits(1<<i));
return 0;
};
-----------snap----------
tk@localhost ~/test $ gcc -O2 -Wall -o test test.c
tk@localhost ~/test $ ./test
IN:0x80000000 OUT:0x00000001
IN:0x40000000 OUT:0x00000002
IN:0x20000000 OUT:0x00000004
IN:0x10000000 OUT:0x00000008
..............
IN:0x00000004 OUT:0x20000000
IN:0x00000002 OUT:0x40000000
IN:0x00000001 OUT:0x80000000

(the printf doesnt take sizeof(int) into account)
printf("IN:0x%. *x OUT:0x%.*x\n", CHAR_BIT * sizeof(int) / 4,
i<<i, CHAR_BIT * sizeof(int) / 4, revbits(1<<i));
Jun 20 '07 #9
On Jun 20, 11:56 am, "Joachim Schmitz" <nospam.j...@sc hmitz-
digital.dewrote :
int reverse_bits(in t n)
{
return ~n;
}

That's inverting (1 turn into 0 and vice versa), not reverting. I understood
the OP wants the laest significant bit being the most significate, the 2nd
least being the second most and so on.
I did not understand that: the OP asked for to "reverse the bits in an
interger". This function reverses all bits in the integer. What you
are proposing is reversing the order of the bits, which is different.

Kind regards,
Johan Borkhuis

Jun 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

47
16581
by: Kapil Khosla | last post by:
Hi, I am trying to reverse a byte eg. 11010000 should look like 00001011 Plz note, it is not a homework problem and I do not need the c code for it. Just give me an idea how should I proceed about it. I know basic bit manipulation , shifting left, right and have done
3
15647
by: Laszlo Szijarto | last post by:
In C#, wha't the best way to reveser the bit order of a data type and then convert it back to that datatype? So, take a byte, reverse bits, convert it back to a byte. I tried to get a BitArray and then call Array.Reverse(), but how to convert back to byte? How about flipping an odd number of bits, say a group of 4 bits, then converting the 4 bits back to an int equivalent? Thanks you in advance for your help,
10
5395
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a string as an input and print it in reverse order.(don't use bulit-in/library function). Sample Output Enter any string: i m Kashif
20
19801
by: mike7411 | last post by:
Is there any easy way to reverse the order of the bits in a byte in C++? (i.e. 00000001 becomes 10000000)
4
6842
by: ranjanmg1 | last post by:
I have a unsigned char.. i need to reverse it.. what the easiest way to do it?? i dont want to tap each bit save and restore etc etc.... Is it possible to perform some bitwise operation which will give the reversed char. e.g., unsigned char x = 0x8A am expecting an output after reversal x = 0x51
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10485
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.