bit operations

Discussion in 'C Programming' started by Carramba, Apr 14, 2005.

  1. Carramba

    Carramba Guest

    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
     
    Carramba, Apr 14, 2005
    #1
    1. Advertising

  2. In article <op.so8k75l3eel3e6@big>, Carramba <> 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
     
    Walter Roberson, Apr 14, 2005
    #2
    1. Advertising

  3. Carramba

    Guest

    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
     
    , Apr 14, 2005
    #3
  4. Carramba

    Carramba Guest

    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, <> 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


    ______________________________
     
    Carramba, Apr 15, 2005
    #4
  5. Carramba

    Jason Curl Guest

    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.
     
    Jason Curl, Apr 15, 2005
    #5
  6. Carramba

    Jason Curl Guest

    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, <> 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
    >>

    >
    >
    >
     
    Jason Curl, Apr 15, 2005
    #6
  7. Carramba

    Richard Bos Guest

    Jason Curl <> 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
     
    Richard Bos, Apr 18, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sanket Suryawanshi

    bit operations in Java

    Sanket Suryawanshi, Jan 23, 2004, in forum: Java
    Replies:
    3
    Views:
    7,077
    Jon A. Cruz
    Jan 23, 2004
  2. Jesus M. Salvo Jr.
    Replies:
    2
    Views:
    4,245
    robert
    Feb 11, 2006
  3. Replies:
    3
    Views:
    1,765
    Timothy Bendfelt
    Jan 19, 2007
  4. Replies:
    9
    Views:
    981
    Juha Nieminen
    Aug 22, 2007
  5. Jeff.M
    Replies:
    6
    Views:
    179
    Lasse Reichstein Nielsen
    May 4, 2009
Loading...

Share This Page