473,387 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Bit manipulation

Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
The result would thus be. 11000111

I am not able to figure this one out.
Can you help ?
Thanks,
kapil
Nov 13 '05 #1
3 3140
On 1 Sep 2003 20:07:24 -0700, kh*********@yahoo.com (Kapil Khosla)
wrote in comp.lang.c:
Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
The result would thus be. 11000111

I am not able to figure this one out.
Can you help ?
Thanks,
kapil


The general rule around here is that we don't write people's code for
them, unless they show us that they have made some effort at doing it
themselves first.

This is actually rather tricky to do in a completely standard,
portable way, but quite simple if you limit portability to
implementations that have no padding bits in their unsigned integer
types.

In any case, I have a short function that will work on any platform
that meets the limitation (not much of a limitation) above.

So you show us yours and I'll show you mine.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
Nov 13 '05 #2

"Kapil Khosla" <kh*********@yahoo.com> wrote in message
news:91**************************@posting.google.c om...
Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
The result would thus be. 11000111

Something like that.

long myfunc(long var, int pos, int size)
{
long mask = 0;
long tmp;
int i;

for(i=pos; i>pos-size; i--)
{
tmp = 1 << (i - 1);
mask = mask | tmp;

}

return var ^ mask;

}

I am not able to figure this one out.
Can you help ?
Thanks,
kapil


--
Jeff
Nov 13 '05 #3
On Wed, 3 Sep 2003 09:34:49 +0800, "Jeff" <no*****@notexist.com>
wrote:

"Kapil Khosla" <kh*********@yahoo.com> wrote in message
news:91**************************@posting.google.c om...
Hi,
I have a problem in which I have to write a function which take
position and number of bits to be modified.
So myfunc(val,pos,size) where val = 11000001, pos = 3 and size = 2
and compliment those bits.
void myfunc () { puts ("Hey! Nice bits!"); }

You meant 'complement'. Or, as correct and easier to spell, 'invert'.
The result would thus be. 11000111

Something like that.

long myfunc(long var, int pos, int size)
{
long mask = 0;
long tmp;
int i;

It's generally best to use unsigned types for bitwise values and
operations.
for(i=pos; i>pos-size; i--)
{
tmp = 1 << (i - 1);
mask = mask | tmp;
Must be 1L /* or better 1UL as I said */ << (i - 1) if any of the
masks to be computed don't fit in int but do in long (which is only
possible of course if long is wider than int).
And, mask |= tmp /* or the expression */ is more idiomatic.
}
(Much) better just /* mask = */ ~(~0UL << size) << (pos-size-1) .
As long as size is strictly less than the width of unsigned long; if
it may not be, either add a special case or use <<1<<(size-1)
(assuming it must be > 0, as is presumably true here) which can be
optimized(?) to ~1UL<<(size-1).

Or possibly substitute ~(~0UL<<size) by a precomputed table.
return var ^ mask;

}

I am not able to figure this one out.
Can you help ?


If this was homework hopefully my reply will be late enough not to
help, not to mention that the OP is apparently lame enough to have no
chance of passing any serious course anyway.

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #4

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

Similar topics

2
by: Marcus | last post by:
I am having some problems with trying to perform calculations on time fields. Say I have a start time and an end time, 1:00:00 and 2:30:00 (on a 24 hour scale, not 12). I want to find the...
4
by: Rune Johansen | last post by:
Hi. I'm doing some image manipulation in an applet using the example code on this page: http://www.akop.org/art/pixels3.htm However, I really want an application rather than an applet, I just...
3
by: Sam | last post by:
Hello, in my coding work I'm going to using a lot of matix manipulation, just basic matrix addition, multiplication, Gaussian method solving for roots, least square... But I don't know if there's...
9
by: I. Kobrinsky | last post by:
I'm new here. I started a personal password-program, a trial that includes username, logincounter and password. So my intention is to hide pwd while tipping. So I'm thinking about two popular...
9
by: Job | last post by:
Hi, I would like to find out what ASP/ASP.net can do with image manipulation. Does ASP have built in functions (eg. after upload to server) to manipulate images, like rotate, scale, crop etc.?...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
8
by: shotokan99 | last post by:
i have this situation. i have a query string: http://www.myquerystring.com?x=xxxxx what this url does is it will return or start downloading a .png file. what i wanted to do is trap this png...
0
by: L'eau Prosper Research | last post by:
Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases new TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set. L'eau Prosper Market...
0
by: L'eau Prosper Research | last post by:
NEW TradeStation 8 Add-on - L'eau Prosper Market Manipulation Profiling Tools Set By L'eau Prosper Research Press Release: L'eau Prosper Research (Website: http://www.leauprosper.com) releases...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.