Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > bit copying

Reply
Thread Tools

bit copying

 
 
curium
Guest
Posts: n/a
 
      08-21-2003
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.



 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      08-22-2003
curium wrote in news:3f45ae9d$0$11384$ :

> 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/
 
Reply With Quote
 
 
 
 
CBFalconer
Guest
Posts: n/a
 
      08-22-2003
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 () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Nudge
Guest
Posts: n/a
 
      08-22-2003
> 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:
&
&&
|
||

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-22-2003
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
 
Reply With Quote
 
Samuel Barber
Guest
Posts: n/a
 
      08-22-2003
Rob Williscroft <> wrote in message news:<Xns93DF48389C834ukcoREMOVEfreenetrtw@195.129 .110.201>...
> curium wrote in news:3f45ae9d$0$11384$ :
>
> > 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
 
Reply With Quote
 
Samuel Barber
Guest
Posts: n/a
 
      08-22-2003
Rob Williscroft <> wrote in message news:<Xns93DF48389C834ukcoREMOVEfreenetrtw@195.129 .110.201>...
> curium wrote in news:3f45ae9d$0$11384$ :
>
> > 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.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-22-2003
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 () ()
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      08-22-2003
On Fri, 22 Aug 2003 11:44:48 GMT, pete <> 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

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-22-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the point of having 16 bit colour if a computer monitor can only display 8 bit colour? How do you edit 16 bit colour when you can only see 8 bit? Scotius Digital Photography 6 07-13-2010 03:33 AM
"LoadLibrary" of a 32 bit so with 64 bit java on a 64 bit machine markryde@gmail.com Java 3 01-19-2007 10:30 PM
Copying files from Window 32 bit to Windows 64 bit Paul D. Olsen Windows 64bit 0 12-29-2005 07:50 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit, Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new ! vvcd Computer Support 0 09-17-2004 08:15 PM
64 bit - Windows Liberty 64bit, Windows Limited Edition 64 Bit,Microsoft SQL Server 2000 Developer Edition 64 Bit, IBM DB2 64 bit - new! Ionizer Computer Support 1 01-01-2004 07:27 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57