c program to set the m-bit to n-bit


 
Thread Tools Search this Thread
Top Forums Programming c program to set the m-bit to n-bit
# 1  
Old 02-24-2012
c program to set the m-bit to n-bit

I have a 32bit number and without using for loop,I want to set mbit to n bit.

Say m bit may be 2nd or 5th or 9th or 10th.n bit may be 22nd or 27or 11th bit.

I assume m<n.

Please help me.Thanks

acdc
# 2  
Old 02-24-2012
It is going to be easy if you write down the steps...pseudocode it etc.
# 3  
Old 02-24-2012
if(X & (1<<M)) X = (X&~(1<<M)) | (1<<N);
# 4  
Old 02-24-2012
Quote:
Originally Posted by Corona688
if(X & (1<<M)) X = (X&~(1<<M)) | (1<<N);
Even shorter...Smilie
Code:
num ^= (1<<m | 1<<n);

# 5  
Old 02-24-2012
That will flip bit N and bit M, not set them. It will also do so no matter what their states are -- it could as easily turn off bit N as on...
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 02-24-2012
Quote:
Originally Posted by Corona688
That will flip bit N and bit M, not set them. It will also do so no matter what their states are -- it could as easily turn off bit N as on...
My bad...I had skimmed the post and thought the op wanted bit m and bit n swapped.
# 7  
Old 02-25-2012
Quote:
Originally Posted by acdc
I have a 32bit number and without using for loop, I want to set mbit to n bit.
...
I assume m<n.
I don't think anyone understood your question, since the suggestions proffered do not reflect what I consider to be the two likeliest interpretations.

Are you trying to set the value of the bit at index m equal to the value of the bit at index n? Or are you trying to set all bits from index m to index n to 1? Or something else entirely?

Why are you not able to use a for loop?

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Which version of Windows Vista to install with a product key? 32-bit or 64-bit?

Hello everyone. I bought a dell laptop (XPS M1330) online which came without a hard drive. There is a Windows Vista Ultimate OEMAct sticker with product key at the bottom case. I checked dell website (here) for this model and it says this model supports both 32 and 64-bit version of Windows... (4 Replies)
Discussion started by: milhan
4 Replies

2. Shell Programming and Scripting

How to handle 64 bit arithmetic operation at 32 bit compiled perl interpreter?H

Hi, Here is the issue. From the program snippet I have Base: 0x1800000000, Size: 0x3FFE7FFFFFFFF which are of 40 and 56 bits. SO I used use bignum to do the math but summing them up I always failed having correct result. perl interpreter info, perl, v5.8.8 built for... (0 Replies)
Discussion started by: rrd1986
0 Replies

3. UNIX for Advanced & Expert Users

migrating unix mp-ras 32 bit to linux suse 64 bit

Hi. I need to migrate the whole unix environment from a Unix mp-ras 32 bit to a Linux Suse 64 bit. 1) can i use cpio to copy the data? 2) can i just copy the users from unix to linux or do i have to create them by hand 3) are there any other concerns i should worry about? thanx (1 Reply)
Discussion started by: mrodrig
1 Replies

4. Red Hat

boot the 32 bit kernel on a 64 bit PPC Linux machine?

Hi all, I'm looking to cover a corner case for an upcoming test cycle. Is there a way to boot a RedHat Advanced Server 4 (update 3) installed on a Power PC machine to use a 32 bit kernel? This would be similar to what is done here -> https://www.unix.com/aix/26204-aix-platform.html I've done... (0 Replies)
Discussion started by: philrau
0 Replies

5. Programming

copying or concatinating string from 1st bit, leaving 0th bit

Hello, If i have 2 strings str1 and str2, i would like to copy/concatenate str2 to str1, from 1st bit leaving the 0th bit. How do i do it? (2 Replies)
Discussion started by: jazz
2 Replies
Login or Register to Ask a Question