How to SetBits 0-4 in a byte using C language

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

    How to SetBits 0-4 in a byte using C language

    Hi all,
    i am new to this forum, but i need a help, i need to increment BIT values of 0-4 without disturbing other bits in a byte, Because others bits 5,6,7 is already set, so i just need to increment the remaining bits whenever and entry is found. for ex when i find a entry, it must increment the bit values from 0-4 without disturbing the rest of the bits. can anybody help me out
  • rajesh6695
    New Member
    • Oct 2006
    • 96

    #2
    Originally posted by thinksmart
    Hi all,
    i am new to this forum, but i need a help, i need to increment BIT values of 0-4 without disturbing other bits in a byte, Because others bits 5,6,7 is already set, so i just need to increment the remaining bits whenever and entry is found. for ex when i find a entry, it must increment the bit values from 0-4 without disturbing the rest of the bits. can anybody help me out

    Try by using bitwise OR
    Example :
    X | 1 will set the 1bit and existing bits will be same only change is 1st bit will set
    X | 2 will set second bit
    X | 3 will set 1st and 2nd bit ......



    Thanks and Regards
    Rajesh G
    rajeshg@infotec hsw.com

    Comment

    • Soujiro
      New Member
      • Jan 2007
      • 35

      #3
      It depends whether big-indian or small indian.. in my case
      for example:
      Code:
                  0010 0000 = (32 in dec ) = (space in ascii)
      you want to leave the first 4 bit as is.. and want to edit the 4 last bit into something like 0010.. this should become 0010 0010.. therefore because

      Code:
                  0010 0010 = (34 in dec ) = "(quote) in ascii
      then thats it.. you've changed the last 4 bit without changing the first 4..



      NOTE: This is what i've done somehow when i was in high school and now im kinda not into that cause im using high level language.. im just here to reminisce C.. Also be careful with unsigned char and char cause they really differ unsigned char can carry up to 255 but char is only up to 127..
      Wait for the powerful ones in the forum they can answer you in a more easy practical way just like the answer above.. :)

      Comment

      • thinksmart
        New Member
        • Jan 2007
        • 8

        #4
        hi,
        sorry for my delay reply, thanks for your reply, but i need to increment the bits 0-4 without disturbing other 3 bits in a byte whenever i find an entry in a list

        Comment

        • thinksmart
          New Member
          • Jan 2007
          • 8

          #5
          Originally posted by rajesh6695
          Try by using bitwise OR
          Example :
          X | 1 will set the 1bit and existing bits will be same only change is 1st bit will set
          X | 2 will set second bit
          X | 3 will set 1st and 2nd bit ......



          Thanks and Regards
          Rajesh G
          rajeshg@infotec hsw.com
          Thanks Rajesh, i need to increment the bits whenever i find an entry in a list, the setting of bits is fine, how to increment???

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            Originally posted by thinksmart
            Thanks Rajesh, i need to increment the bits whenever i find an entry in a list, the setting of bits is fine, how to increment???
            the following should increment bits 0-3 of n without effecting bits 4-7
            Code:
                   int temp=(n&0xf);  // get bits 0-3   
                   // increment bits 0 to 3 and add in bits 4-7
                   n=(n & 0xf0) + ((++temp) & 0xf);

            Comment

            • thinksmart
              New Member
              • Jan 2007
              • 8

              #7
              Originally posted by horace1
              the following should increment bits 0-3 of n without effecting bits 4-7
              Code:
                     int temp=(n&0xf);  // get bits 0-3   
                     // increment bits 0 to 3 and add in bits 4-7
                     n=(n & 0xf0) + ((++temp) & 0xf);
              hi thank you for your suggestion, its working fine, i just modified
              thank you all for your help

              Comment

              Working...