How to set bits 4-7 without changing bits 0-3

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thinksmart
    New Member
    • Jan 2007
    • 8

    How to set bits 4-7 without changing bits 0-3

    Hi all,
    I am gettting an unsigned integer from a table and i have to set that integer to a byte from the bits 4-7 since the bits 0-3 is already occupied, i dont want to disturb the bits 0-3 . the integer value is always less than 8. Can any1 help me out by simple example
  • thinksmart
    New Member
    • Jan 2007
    • 8

    #2
    i think i need to give still a better explanation, my bit position is like this 0000 0111 , now i need to get an integer which is less than 8 and if it is 5 it shud be like this 0101 0111. my 0111 should not be disturbed.

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      Originally posted by thinksmart
      Hi all,
      I am gettting an unsigned integer from a table and i have to set that integer to a byte from the bits 4-7 since the bits 0-3 is already occupied, i dont want to disturb the bits 0-3 . the integer value is always less than 8. Can any1 help me out by simple example
      have a look at
      http://www.thescripts. com/forum/thread590930.ht ml

      Comment

      • thinksmart
        New Member
        • Jan 2007
        • 8

        #4
        Originally posted by horace1
        have a look at
        http://www.thescripts. com/forum/thread590930.ht ml
        y= ((x >> 4)& 0x0f) | ((x << 4) & 0xf0);

        ???? can you explain me please abt the above code, thanks in advance

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          Originally posted by thinksmart
          y= ((x >> 4)& 0x0f) | ((x << 4) & 0xf0);

          ???? can you explain me please abt the above code, thanks in advance
          we are swoping bits 0 to 3 and bits 4 to 7 of a byte
          Code:
          ((x >> 4)& 0x0f)
          shifts x 4 bits right and ANDs by 0x0f to get bits 0 to 3

          Code:
          ((x << 4) & 0xf0)
          shifts x 4 bits left and ANDs by 0xf0 to get bits 4 to 7

          the result of the above operations is then ORed together to get bits 0 to 7
          Code:
          y= ((x >> 4)& 0x0f) | ((x << 4) & 0xf0);
          and the result is assigned to y

          Comment

          • thinksmart
            New Member
            • Jan 2007
            • 8

            #6
            Originally posted by horace1
            we are swoping bits 0 to 3 and bits 4 to 7 of a byte
            Code:
            ((x >> 4)& 0x0f)
            shifts x 4 bits right and ANDs by 0x0f to get bits 0 to 3

            Code:
            ((x << 4) & 0xf0)
            shifts x 4 bits left and ANDs by 0xf0 to get bits 4 to 7

            the result of the above operations is then ORed together to get bits 0 to 7
            Code:
            y= ((x >> 4)& 0x0f) | ((x << 4) & 0xf0);
            and the result is assigned to y
            Thank you very much my friend, i got the required answer, thanks a lot

            Comment

            Working...