Quick question regardin bit shifting

This is a discussion on Quick question regardin bit shifting within the C Programming forums, part of the General Programming Boards category; im trying to shift two bytes of "binary" to OR them together and allow a carry from byte1 to be ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    Quick question regardin bit shifting

    im trying to shift two bytes of "binary" to OR them together and allow a carry from byte1 to be placed into byte2.

    at the moment, im trying to shift byte1 by 7 places to the RIGHT and byte2 by 1 place to the LEFT.

    i need to use the logical shift and not the arithmetic one so could someone look at the couple of lines of code below to tell me why, when i or together two bytes i get the wrong answer.

    Code:
    firstbyte[0] = (squashdum[1] >>= 7);
    Code:
    secondbyte[0] = squashdum[2] <<= 1;
    Code:
    result[1] = (firstbyte[0] | secondbyte[0]);
    imagine first byte is 01100101
    and second byte is 10000111

    the result should be "00001110". = e0

    but actually comes through as "00001111"? = f0

    thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So what value does 'squashed' have in it first? At any rate:

    01100101 >> 1 = 00110010
    00110010 >> 1 = 00011001
    00011001 >> 1 = 00001100
    00001100 >> 1 = 00000110
    00000110 >> 1 = 00000011
    00000011 >> 1 = 00000001

    Seven shifts.

    10000111 << 1 = 00001110

    One shift.

    00001110 | 00000001 = 00001111


    See?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by quzah
    Seven shifts.
    Looks like 6.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hm. Could be. I was thinking when I was working it out that their end result was wrong. Looks like I was right after all.

    So yeah, anyway, your:
    a) Input is wrong, or...
    b) Your output is wrong, as to what you're telling is you're getting.

    Because if you did shift that seven places, and not 6, your result would be zero for the first portion.


    Quzah.
    Last edited by quzah; 11-03-2005 at 08:41 AM.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    well.

    im inputting a string of hexcharacters "1234567890" into a function which "squashes" them so that
    Code:
    squashdum[0] = "00010010" = "12"
    squashdum[1] = "11000010" = "34"
    and pairs of numbers/hex thereafter.

    the MSB in squashdum[0] must be Or'd with squashdum[1]. at the moment, im doing only ONE shift.

    im starting to think that its maybe to do with the numbering of the indexes of the array and the arrangement of the binary stack within it.

    i.e. im thinking that the string in contains the values as...

    Code:
    squashdum[2] squashdum[1] squashdum[0]
    by my logic would equate to a "binary" of within the array

    Code:
    01100101 ||| 01000011 ||| 00100001
    the aim is to insert a zero or maybe 5 zero's at the start of the string "squashdum[0]" and watch the bits move along. if they fall off the edge then they just move to the next byte.

    is this correct or have i confused myself?
    Last edited by shoobsie; 11-03-2005 at 09:19 AM.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    I'm having trouble with:
    Code:
    squashdum[1] = "11000010" = "34"
    Wouldn't that binary number represent the two 4-bit integers 12 (0xC) and 2?

    1 and 2 in squashdum[0] looks right, but that one looks weird. 3 and 4 should be 00110100.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    i just spoke to a friend and he told me the order of bytes and power of bits is:

    [0][1][2][3]

    within which is

    [0]MSB.........LSB[0] [1]MSB.........LSB[1]

    hope this is right now!

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    You can express binary numbers either way, but you have to express them consistently or you're going to get confused. squashdum[0] shows MSB -> LSB, but squashdum[1] shows LSB -> MSB. Typically I've seen MSB -> LSB so you have:

    1 - 0001
    2 - 0010
    3 - 0011
    etc.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by itsme86
    You can express binary numbers either way, but you have to express them consistently or you're going to get confused. squashdum[0] shows MSB -> LSB, but squashdum[1] shows LSB -> MSB. Typically I've seen MSB -> LSB so you have:

    1 - 0001
    2 - 0010
    3 - 0011
    etc.
    excellent.

    its just that in my earlier post, i had them the wrong way round.

    i was actually asking if i was correct!....which i wasnt

    thanks alot though.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    i've found that the problem is that when i shift the following string left by 1 position or multiply it by two:

    i get the answer F0 when it should be E0. i think the program is adding and an extra 1 instead of a 0 at the least significant bit, after the shift.

    so.....

    Code:
    1000 0111
    should be

    Code:
    0000 1110
    after 1 shift left.

    but i get

    Code:
    0000 1111
    how do i set the LSB to zero? was thinking of an AND mask with 0xFE but would that work?

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    I get the right answer. I'm not sure what you're doing wrong...
    Code:
    itsme@itsme:~/C$ ./shift
    10000111 - 87
    00001110 - 0E
    itsme@itsme:~/C$ cat shift.c
    #include <stdio.h>
    
    void show_it(int num)
    {
      int i;
    
      for(i = 7;i >= 0;--i)
        printf("%c", (num >> i) & 1 ? '1' : '0');
      printf(" - %02X\n", num);
    }
    
    int main(void)
    {
      unsigned char num = 0x87;
    
      show_it(num);
      num <<= 1;
      show_it(num);
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./shift
    10000111 - 87
    00001110 - 0E
    itsme@itsme:~/C$
    how do i set the LSB to zero? was thinking of an AND mask with 0xFE but would that work?
    Yes, but you shouldn't need it. I don't see how that 1 is slipping in.
    Last edited by itsme86; 11-04-2005 at 09:48 AM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. bit operation question
    By mr_coffee in forum C Programming
    Replies: 3
    Last Post: 04-07-2009, 05:00 PM
  2. Question about Bit Manipulation.
    By parastar in forum C Programming
    Replies: 5
    Last Post: 01-28-2009, 07:46 AM
  3. Bit shifting with matrices
    By OnionKnight in forum C Programming
    Replies: 9
    Last Post: 02-09-2005, 08:53 PM
  4. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21