Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > bit operations

Reply
Thread Tools

bit operations

 
 
Carramba
Guest
Posts: n/a
 
      04-14-2005
hi!

Iam want to split 8 bites to 2 groups of 4 bites
so get get char with
biteVariabel=getchar();
firsFourBites=biteVariabel<<4;
lastFourBites=biteVariabel>>4;

but I get only lastFourBites writte... why is that?


--
thanx in advance

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      04-14-2005
In article <op.so8k75l3eel3e6@big>, Carramba <(E-Mail Removed)> wrote:
>Iam want to split 8 bites to 2 groups of 4 bites
>so get get char with
>biteVariabel=getchar();
>firsFourBites=biteVariabel<<4;
>lastFourBites=biteVariabel>>4;


>but I get only lastFourBites writte... why is that?


I believe the English word you are looking for is 'bits' instead of 'bites'
(which in English would be pronounced close the the same way as 'bytes'
which would lead to confusion.)

Could you clarify what you mean by "but I get only lastFourBites writte"?
The code you show does not write out anything at all.

Also, how is biteVariabel defined? To avoid undefined behaviour it
should probably be defined as an unsigned char . It is better not
to assume that the local character is unsigned. Right-shifting
a signed value that has its top bit set has implementation-
defined behaviour.
--
"[...] it's all part of one's right to be publicly stupid." -- Dave Smey
 
Reply With Quote
 
 
 
 
lndresnick@gmail.com
Guest
Posts: n/a
 
      04-14-2005

Carramba wrote:
> hi!
>
> Iam want to split 8 bites to 2 groups of 4 bites
> so get get char with
> biteVariabel=getchar();
> firsFourBites=biteVariabel<<4;
> lastFourBites=biteVariabel>>4;
>
> but I get only lastFourBites writte... why is that?
>
>
> --
> thanx in advance


I'm not sure exactly what you are trying to do here or what
results you are getting.

Does this reflect your aims?

(1270)$ cat foo.c
#include <stdio.h>
int main(void)
{
unsigned char value = 0xC5;
unsigned char hibits = (value & ~0xF) >> 4;
unsigned char lobits = value & ~0xF0;
printf("%X %X %X\n", value, hibits, lobits);
return 0;
}
temp(1271)$ foo
C5 C 5

Note that this assumes a char is 8 bits, but that fits with the
problem you describe.

-David

 
Reply With Quote
 
Carramba
Guest
Posts: n/a
 
      04-15-2005
thanx matte!

It was exactly what I mean, and Iam sorry for my bad english....
I have just one more question about you code,
is it general solution to divide 8 bits into 2groups of four, by 'hiding'
first four bits and then shifting to right. or I have missunderstod?

thanx again!

On Thu, 14 Apr 2005 22:05:09 +0200, <(E-Mail Removed)> wrote:

>
> Carramba wrote:
>> hi!
>>
>> Iam want to split 8 bites to 2 groups of 4 bites
>> so get get char with
>> biteVariabel=getchar();
>> firsFourBites=biteVariabel<<4;
>> lastFourBites=biteVariabel>>4;
>>
>> but I get only lastFourBites writte... why is that?
>>
>>
>> --
>> thanx in advance

>
> I'm not sure exactly what you are trying to do here or what
> results you are getting.
>
> Does this reflect your aims?
>
> (1270)$ cat foo.c
> #include <stdio.h>
> int main(void)
> {
> unsigned char value = 0xC5;
> unsigned char hibits = (value & ~0xF) >> 4;
> unsigned char lobits = value & ~0xF0;
> printf("%X %X %X\n", value, hibits, lobits);
> return 0;
> }
> temp(1271)$ foo
> C5 C 5
>
> Note that this assumes a char is 8 bits, but that fits with the
> problem you describe.
>
> -David
>




--
thanx in advance


______________________________
 
Reply With Quote
 
Jason Curl
Guest
Posts: n/a
 
      04-15-2005
Carramba wrote:
> hi!
>
> Iam want to split 8 bites to 2 groups of 4 bites
> so get get char with
> biteVariabel=getchar();
> firsFourBites=biteVariabel<<4;
> lastFourBites=biteVariabel>>4;
>
> but I get only lastFourBites writte... why is that?
>
>

Try writing a byte as

76543210

which are the positions of each bit. Then with the shift operators,
anything the moves off to the left/right is lost and a new bit is given
a zero.

Then you'll understand why you're getting the results you explain.
 
Reply With Quote
 
Jason Curl
Guest
Posts: n/a
 
      04-15-2005
Carramba wrote:
> thanx matte!
>
> It was exactly what I mean, and Iam sorry for my bad english....
> I have just one more question about you code,
> is it general solution to divide 8 bits into 2groups of four, by
> 'hiding' first four bits and then shifting to right. or I have
> missunderstod?


When shifting a byte to the right, it is not necessary to "hide" (the
term is "masking") the upper bits if it's only a byte your interested in
(e.g. a number in the range from 0 to 255). But it doesn't hurt, it is
definitely required if you need to access bits within a "word" that is
two bytes long.

lastFourBites=biteVariabel>>4;

76543210
hhhhllll <- Before the shift
0000hhhh <- After the shift


firsFourBites=biteVariabel & 0xF;

76543210
hhhhllll <- Before the mask
0000llll <- After the mask

By the way, the name for a group of four bits is called a "nibble".

You should be able to find good resources on "bit manipulation" and "bit
operators" such as "and", "or", "xor" (or "eor").

In C:

bitwise and is &
bitwise or is |
bitwise xor is ^
bitwise not is ~

Note, this give different results for

logical and is &&
logical or is ||
logical not is !

>
> thanx again!
>
> On Thu, 14 Apr 2005 22:05:09 +0200, <(E-Mail Removed)> wrote:
>
>>
>> Carramba wrote:
>>
>>> hi!
>>>
>>> Iam want to split 8 bites to 2 groups of 4 bites
>>> so get get char with
>>> biteVariabel=getchar();
>>> firsFourBites=biteVariabel<<4;
>>> lastFourBites=biteVariabel>>4;
>>>
>>> but I get only lastFourBites writte... why is that?
>>>
>>>
>>> --
>>> thanx in advance

>>
>>
>> I'm not sure exactly what you are trying to do here or what
>> results you are getting.
>>
>> Does this reflect your aims?
>>
>> (1270)$ cat foo.c
>> #include <stdio.h>
>> int main(void)
>> {
>> unsigned char value = 0xC5;
>> unsigned char hibits = (value & ~0xF) >> 4;
>> unsigned char lobits = value & ~0xF0;
>> printf("%X %X %X\n", value, hibits, lobits);
>> return 0;
>> }
>> temp(1271)$ foo
>> C5 C 5
>>
>> Note that this assumes a char is 8 bits, but that fits with the
>> problem you describe.
>>
>> -David
>>

>
>
>

 
Reply With Quote
 
Richard Bos
Guest
Posts: n/a
 
      04-18-2005
Jason Curl <(E-Mail Removed)> wrote:

[ Everybody... please learn to snip. ]

> Carramba wrote:
> > thanx matte!
> >
> > It was exactly what I mean, and Iam sorry for my bad english....
> > I have just one more question about you code,
> > is it general solution to divide 8 bits into 2groups of four, by
> > 'hiding' first four bits and then shifting to right. or I have
> > missunderstod?

>
> When shifting a byte to the right, it is not necessary to "hide" (the
> term is "masking") the upper bits if it's only a byte your interested in
> (e.g. a number in the range from 0 to 255). But it doesn't hurt, it is
> definitely required if you need to access bits within a "word" that is
> two bytes long.
>
> lastFourBites=biteVariabel>>4;
>
> 76543210
> hhhhllll <- Before the shift
> 0000hhhh <- After the shift


Be careful, though: this is only reliably true for unsigned types. For
negative signed values, the result is implementation-defined.

Richard
 
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
stand-alone JMS, other JDBC operations, and transactions ( ActiveMQ + JOTM + JDBC operations ) Jesus M. Salvo Jr. Java 2 02-11-2006 06:33 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