Shifted bits

This is a discussion on Shifted bits within the C Programming forums, part of the General Programming Boards category; I am using 4-byte integers to perform various logical functions and what I want to be able to do is ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Shifted bits

    I am using 4-byte integers to perform various logical
    functions and what I want to be able to do is save any overflow or shifted right or left bits after I have peformed a particular function eg


    F = (H'12345678 << 23);

    F would then equal H'9E000000

    How can I save the shifted (lost) bits efficiently for manipulation later on.

    This must be easy I know...but I cannot see it


    Thanks - Ragnam

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Shift it the opposite direction first, then back. Place that value in a seperate variable. Consider:

    10101101

    We want a 3 shift left. Shift right 5 first:

    00000101

    Shift it back.

    10100000

    Store that in a variable. Use N-X bytes, where N is the number of bytes in the variable, and X is the number you're shifting, to get your value of how far to shift in the opposite direction and back.

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

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Smile

    Thanks for that Quzah.

    Can this method be used to also test for a carry-bit when
    two numbers are added together ?

    eg H'EA000000 +
    H'D6000000
    ---------------
    H'1 C0000000

    Using a 4-byte integer would normally lose the carry '1'

    Ragnam

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why do you keep using an H and an apostrophe in your numbers? What is that supposed to denote? That is nothing in C.

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

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    It was for demonstration only, that the values were hex....sorry.

    F = (0xEA000000 + 0xD6000000)

    F = 0xC0000000

    Using a 4-byte integer would normally lose the carry '1'

    Ragnam

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  2. SDLKey to ASCII without unicode support?
    By zacs7 in forum Game Programming
    Replies: 6
    Last Post: 10-07-2007, 03:03 AM
  3. Help counting number of bits set in an integer
    By JayDiddums10 in forum C Programming
    Replies: 5
    Last Post: 12-07-2006, 02:21 PM
  4. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM
  5. New idea on conveting byte to bits/bits to byte
    By megablue in forum C Programming
    Replies: 10
    Last Post: 10-26-2003, 01:16 AM

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