By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,421 Members | 1,127 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,421 IT Pros & Developers. It's quick & easy.

bit copying

P: n/a
Hi

I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i need to
copy bits 0-6 from another byte into bits 7-14 of this word.

I am going gray from messing around with AND, OR, >> and << operators. I
hope the collective audience of this group can point out an obvious solution
to this problem so that i can kick myself for not seeing the obvious.

The main problem i am experiencing is preserving the LO byte of the word
when copying from the 2nd byte.

Thanks.

Jul 19 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
curium wrote in news:3f***********************@news.dial.pipex.com :
Hi

I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
need to copy bits 0-6 from another byte into bits 7-14 of this word.

I am going gray from messing around with AND, OR, >> and << operators.
I hope the collective audience of this group can point out an obvious
solution to this problem so that i can kick myself for not seeing the
obvious.

The main problem i am experiencing is preserving the LO byte of the
word when copying from the 2nd byte.


unsigned merge( unsigned char lo, unsigned char hi )
{
unsigned result = static_cast< unsigned >(lo) & 0x3F;

result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;

return rsult;
}
HTH

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 19 '05 #2

P: n/a
Rob Williscroft wrote:
curium wrote:
I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
need to copy bits 0-6 from another byte into bits 7-14 of this word.

I am going gray from messing around with AND, OR, >> and <<
operators. I hope the collective audience of this group can point
out an obvious solution to this problem so that i can kick myself
for not seeing the obvious.

The main problem i am experiencing is preserving the LO byte of
the word when copying from the 2nd byte.


unsigned merge( unsigned char lo, unsigned char hi )
{
unsigned result = static_cast< unsigned >(lo) & 0x3F;

result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;

return rsult;
}


Because some idiot cross posted to c.l.c and c.l.c++, you have
posted something that is not applicable on c.l.c. So lets change
the function body to be valid C, and hope it is still valid C++
:-)

unsigned int result;

result = ((hi & 0x3f) << 6) | (lo & 0x3f);
return result;

These values can never cause an integer overflow. There is no
such thing as an AND or an OR operator.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Jul 19 '05 #3

P: n/a
> I don't understand what you mean by
"There is no such thing as an AND or an OR operator."


AND and OR and not defined as keywords in C.

You'd use one of:
&
&&
|
||

Jul 19 '05 #4

P: n/a
Nudge wrote:
I don't understand what you mean by
"There is no such thing as an AND or an OR operator."
AND and OR and not defined as keywords in C.


I can understand that well enough to say that
that makes no sense at all.
There's a lot more to C, than just keywords.
You'd use one of:
& N868
6.5.10 Bitwise AND operator
&& N869
6.5.13 Logical AND operator
| N868
6.5.12 Bitwise OR operator

||

N869
6.5.14 Logical OR operator

--
pete
Jul 19 '05 #5

P: n/a
Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in message news:<Xn**********************************@195.129 .110.201>...
curium wrote in news:3f***********************@news.dial.pipex.com :
I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
need to copy bits 0-6 from another byte into bits 7-14 of this word.

I am going gray from messing around with AND, OR, >> and << operators.
I hope the collective audience of this group can point out an obvious
solution to this problem so that i can kick myself for not seeing the
obvious.


unsigned merge( unsigned char lo, unsigned char hi )
{
unsigned result = static_cast< unsigned >(lo) & 0x3F;

result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;

return result;
}


Or if I understood the problem description, this:

unsigned merge( unsigned result, unsigned char lo, unsigned char hi )
{
result &= ~( (1<<14) - 1 );

result |= lo & 0x3F;

result |= ( hi & 0x3F ) << 6;

return result;
}

The casts are not needed.

Sam
Jul 19 '05 #6

P: n/a
Rob Williscroft <rt*@freenet.REMOVE.co.uk> wrote in message news:<Xn**********************************@195.129 .110.201>...
curium wrote in news:3f***********************@news.dial.pipex.com :
I need to copy bits 0-6 from a byte into bits 0-6 of a word. then i
need to copy bits 0-6 from another byte into bits 7-14 of this word.

I am going gray from messing around with AND, OR, >> and << operators.
I hope the collective audience of this group can point out an obvious
solution to this problem so that i can kick myself for not seeing the
obvious.


unsigned merge( unsigned char lo, unsigned char hi )
{
unsigned result = static_cast< unsigned >(lo) & 0x3F;

result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;

return rsult;
}


Or if I understood the problem description, this:

unsigned merge( unsigned result, unsigned char lo, unsigned char hi )
{
result &= ~( (1<<14) - 1);

result |= lo & 0x7F;

result |= ( hi & 0x7F ) << 7;

return result;
}

The casts are not needed.

Sam

P.S. Ignore my other reply.
Jul 19 '05 #7

P: n/a
pete wrote:
CBFalconer wrote:
Rob Williscroft wrote:
curium wrote:

> I need to copy bits 0-6 from a byte into bits 0-6 of a word.
> then i need to copy bits 0-6 from another byte
> into bits 7-14 of this word.
>
> I am going gray from messing around with AND, OR, >> and <<
> operators. I hope the collective audience of this group can
> point out an obvious solution to this problem so that i can
> kick myself for not seeing the obvious.
>
> The main problem i am experiencing is preserving the LO byte
> of the word when copying from the 2nd byte.

unsigned merge( unsigned char lo, unsigned char hi )
{
unsigned result = static_cast< unsigned >(lo) & 0x3F;

result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
return rsult;
}


Because some idiot cross posted to c.l.c and c.l.c++, you have
posted something that is not applicable on c.l.c. So lets change
the function body to be valid C, and hope it is still valid C++
:-)

unsigned int result;

result = ((hi & 0x3f) << 6) | (lo & 0x3f);
return result;

These values can never cause an integer overflow. There is no
such thing as an AND or an OR operator.


I don't understand what you mean by
"There is no such thing as an AND or an OR operator."


The operators are || and &&, unless you #include <iso646.h>, when
the macros 'or' and 'and' expand to those anyway. That also give
the equivalence of 'bit_and' and &, and of 'bit_or' and |.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Jul 19 '05 #8

P: n/a
On Fri, 22 Aug 2003 11:44:48 GMT, pete <pf*****@mindspring.com> wrote:
> I don't understand what you mean by
> "There is no such thing as an AND or an OR operator."


AND and OR and not defined as keywords in C.


I can understand that well enough to say that
that makes no sense at all.
There's a lot more to C, than just keywords.


I think you're confusing operators and operations.

The set of C operators is quite small, and does not include either AND
or OR.

--
Al Balmer
Balmer Consulting
re************************@att.net
Jul 19 '05 #9

P: n/a
CBFalconer wrote:

pete wrote:
CBFalconer wrote:
Rob Williscroft wrote:
> curium wrote:
>
> > I need to copy bits 0-6 from a byte into bits 0-6 of a word.
> > then i need to copy bits 0-6 from another byte
> > into bits 7-14 of this word.
> >
> > I am going gray from messing around with AND, OR, >> and <<
> > operators. I hope the collective audience of this group can
> > point out an obvious solution to this problem so that i can
> > kick myself for not seeing the obvious.
> >
> > The main problem i am experiencing is preserving the LO byte
> > of the word when copying from the 2nd byte.
>
> unsigned merge( unsigned char lo, unsigned char hi )
> {
> unsigned result = static_cast< unsigned >(lo) & 0x3F;
>
> result |= ( static_cast< unsigned >(hi) & 0x3F ) << 6;
> return rsult;
> }

Because some idiot cross posted to c.l.c and c.l.c++, you have
posted something that is not applicable on c.l.c. So lets change
the function body to be valid C, and hope it is still valid C++
:-)

unsigned int result;

result = ((hi & 0x3f) << 6) | (lo & 0x3f);
return result;

These values can never cause an integer overflow. There is no
such thing as an AND or an OR operator.


I don't understand what you mean by
"There is no such thing as an AND or an OR operator."


The operators are || and &&,
unless you #include <iso646.h>, when
the macros 'or' and 'and' expand to those anyway.


I'm having a very hard time parsing that sentence.

You're saying that when you #include <iso646.h>,
then || and && aren't the operators, but that when you
don't #include <iso646.h>, then they are the operators.

I'm even more confused about what you're saying now.
What do you mean by "The", in "The operators" ?
There's no || or && in the posted code,
so you must mean that those operators have been previously
referred to in the text of this thread.
*Which* operators are || and && ?
The ones which there are no such things as ?

Is your objection that he did not precisely specify
"bitwise AND operator"
and
"bitwise inclusive OR assignment operator"
?

--
pete
Jul 19 '05 #10

P: n/a
>>> > I don't understand what you mean by
> "There is no such thing as an AND or an OR operator."

AND and OR and not defined as keywords in C.


I can understand that well enough to say that
that makes no sense at all.
There's a lot more to C, than just keywords.


I think you're confusing operators and operations.

The set of C operators is quite small, and does not include either AND
or OR.


But & and |

--
- Jan Engelhardt
Jul 19 '05 #11

P: n/a
Alan Balmer wrote:

On Fri, 22 Aug 2003 11:44:48 GMT, pete <pf*****@mindspring.com> wrote:

> I don't understand what you mean by
> "There is no such thing as an AND or an OR operator."

AND and OR and not defined as keywords in C.
I can understand that well enough to say that
that makes no sense at all.
There's a lot more to C, than just keywords.


I think you're confusing operators and operations.


What do you mean ?
The set of C operators is quite small,
There are over 40 operators in C.
and does not include either AND or OR.


I'll admit that "AND" is too brief,
to completely specify which operator it refers to.

--
pete
Jul 19 '05 #12

P: n/a
Rob Williscroft wrote:

Samuel Barber wrote in
news:37**************************@posting.google.c om:
Or if I understood the problem description, this:

unsigned merge( unsigned result, unsigned char lo, unsigned char hi )
{
result &= ~( (1<<14) - 1);

result |= lo & 0x7F;

result |= ( hi & 0x7F ) << 7;

return result;
}

The casts are not needed.


In a *nice* little function with unsigned args, I guess so.


That's the way that I would write it (with unsigned args).

unsigned merge( unsigned result, unsigned lo, unsigned hi);

.... regardles of the fact that the representations of values
for lo and hi, fit into bytes.
That way, the variables don't get converted in any of
the operations in that function.
I realise that the conversion of variables isn't quite tragic,
but the function is simpler without it.

--
pete
Jul 19 '05 #13

P: n/a
On Fri, 22 Aug 2003 16:21:17 GMT, pete <pf*****@mindspring.com> wrote:
Alan Balmer wrote:

On Fri, 22 Aug 2003 11:44:48 GMT, pete <pf*****@mindspring.com> wrote:
>>
>> > I don't understand what you mean by
>> > "There is no such thing as an AND or an OR operator."
>>
>> AND and OR and not defined as keywords in C.
>
>I can understand that well enough to say that
>that makes no sense at all.
>There's a lot more to C, than just keywords.
I think you're confusing operators and operations.


What do you mean ?


Exactly what I said.
The set of C operators is quite small,
There are over 40 operators in C.


Yes. A small set.
and does not include either AND or OR.


I'll admit that "AND" is too brief,
to completely specify which operator it refers to.


Again, you're referring to a *description* of the operator, not the
operator itself, which in this instance is even more brief - one of
'&' or '&&'.

If you like, you can write a paragraph describing what '&&' does, but
that paragraph will not be an operator.

--
Al Balmer
Balmer Consulting
re************************@att.net
Jul 19 '05 #14

P: n/a
Alan Balmer wrote:

On Fri, 22 Aug 2003 16:21:17 GMT, pete <pf*****@mindspring.com> wrote:
Alan Balmer wrote:

On Fri, 22 Aug 2003 11:44:48 GMT,
pete <pf*****@mindspring.com> wrote:
I'll admit that "AND" is too brief,
to completely specify which operator it refers to.
Again, you're referring to a *description* of the operator, not the
operator itself, which in this instance is even more brief - one of
'&' or '&&'.


What you are calling the "description of the operator",
(Bitwise AND operator)
I am calling the "name of the operator",
and my point was that the full name of the operator wasn't given.
If you like, you can write a paragraph describing what '&&' does, but
that paragraph will not be an operator.


I think you're making a distinction between
names and descriptions and subjects, which is inappropriate.
What you wrote, is as obvious as saying that
if I write a paragraph about you, then that paragraph isn't you.

However if I said that "the guy I'm arguing with" is wrong,
or if I said "Alan Balmer" is wrong,
then that's the same thing as saying that you are wrong.

--
pete
Jul 19 '05 #15

P: n/a
pete wrote:
CBFalconer wrote:
.... snip ...
The operators are || and &&,
unless you #include <iso646.h>, when
the macros 'or' and 'and' expand to those anyway.


I'm having a very hard time parsing that sentence.

You're saying that when you #include <iso646.h>,
then || and && aren't the operators, but that when you
don't #include <iso646.h>, then they are the operators.


When you include iso646.h, you have the option of using the word
'and' to signify &&, and the word 'or' to signify ||. Note
option. && and || still work as advertised.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Jul 19 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.