bit copying

C

curium

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.
 
R

Rob Williscroft

curium wrote in
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.
 
C

CBFalconer

Rob said:
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.
 
N

Nudge

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:
&
&&
|
||
 
S

Samuel Barber

Rob Williscroft said:
curium wrote in

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
 
S

Samuel Barber

Rob Williscroft said:
curium wrote in

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.
 
C

CBFalconer

pete said:
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 |.
 
A

Alan Balmer

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.
 
P

pete

CBFalconer said:
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"
?
 
J

Jan Engelhardt

I don't understand what you mean by
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 |
 
P

pete

Rob said:
Samuel Barber wrote in


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.
 
A

Alan Balmer

What do you mean ?

Exactly what I said.
There are over 40 operators in C.

Yes. A small set.
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.
 
P

pete

Alan said:
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.
 
C

CBFalconer

pete said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top