bit operations

C

Carramba

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?
 
W

Walter Roberson

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

lndresnick

Carramba said:
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?

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
 
C

Carramba

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!
 
J

Jason Curl

Carramba said:
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.
 
J

Jason Curl

Carramba said:
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 !
 
R

Richard Bos

[ Everybody... please learn to snip. ]
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
 

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,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top