Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Shifting bytes

Shifting bytes

Scheduled Pinned Locked Moved C#
questioncsharphtmlcomdata-structures
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hadad
    wrote on last edited by
    #1

    Hello, Now I have an array of 7 bits bytes or a string I want to take the last bit from the second byte and add it to the right most of the first and so on which will result in a new 8 bits byte Is there any direct way to do that than using loops and arrays? Example http://www.dreamfabric.com/sms/hello.html How can I do this by c#? Thanks.

    Dad

    I 1 Reply Last reply
    0
    • H hadad

      Hello, Now I have an array of 7 bits bytes or a string I want to take the last bit from the second byte and add it to the right most of the first and so on which will result in a new 8 bits byte Is there any direct way to do that than using loops and arrays? Example http://www.dreamfabric.com/sms/hello.html How can I do this by c#? Thanks.

      Dad

      I Offline
      I Offline
      Ian Shlasko
      wrote on last edited by
      #2

      Shouldn't be too difficult to figure out an algorithm for it... Some hints: 1) To check the rightmost bit of a number: (x % 2) // Remainder when dividing by 2) The generic form would be (x % (1 << numBits)) to get any number of bits. 2) To remove the rightmost bit: (x >> 1) // Shift right one bit 3) To add a bit to the left of a number: (x + (newBit << currentBitCount)) As for the algorithm, you know exactly how many bits are moving into and out of each byte... Each group of eight septets turns into 7 octets... So the formulae are: oct0 = s0 + ((s1 % (1 << 1)) << 7) (Start with septet 0, get 1 bit from septet 1, shift it 7 places left, and add it to septet 0) oct1 = (s1 >> 1) + ((s2 % (1 << 2)) << 6) (Shift out one bit, since we moved it to octet 0, grab 2 bits from the next byte, shift 6 places, since we're one place shorter, and add) oct3 = (s2 >> 2) + ((s3 % (1 << 3)) << 5) See the pattern? (A = this byte, B = next byte) octN = (A >> N) + ((B % (1 << (N + 1))) << (7 - N)) Do that for 7 bytes, skip the 8th byte (All 7 bits from the 8th byte would be shifted to the 7th byte), and start over.

      Proud to have finally moved to the A-Ark. Which one are you in? Developer, Author (Guardians of Xen)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups