Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Rotating bits during a left bit shift?

  • 16-03-2008 7:19pm
    #1
    Closed Accounts Posts: 12,382 ✭✭✭✭


    Hello

    I've googled this and I cannot find anything.

    If I want to perform one left shift on the following:

    10001111

    How do I make the 1 rotate to the end of the byte so it looks like:

    00011111

    Any ideas? :confused:

    Thank you.


Comments

  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    Take it x86 ASM?
    Of course you could embed it into C

    Roll Instructions
    In a roll instruction, the bits that slide off the end of the register are fed back into the spaces.

    ror arg
    roll to the right

    rol arg
    roll to the left


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Thanks webmonkey.

    I'm actually doing it in C.

    When you say, "embed it in C", how exactly would I do that?

    I'm back new with C after years of PHP, so I'm still getting my head around actual real logic :)

    Thanks


  • Registered Users, Registered Users 2 Posts: 9,579 ✭✭✭Webmonkey


    You can embed assembly into C by using the

    asm
    {
    //ASM Code
    }

    Being a while with C now but what exactly are you trying to do, If its an array, you could just temporairly copy the 1st bit somewhere, then copy the next bit over the current bit and then at the end place the copied bit to the end.

    If its a register on the processor or a known memory location you could do it low level with the assembly.


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    It's an array.

    I have a four byte array. I want to left shift some bits from the first byte onto the end of the last byte.

    So...

    11111111 11111111 11111111 00000000

    Shifted twice:

    11111111 11111111 11111100 00000011

    I'll try your suggestion (keeping a temporary copy of the bit, shifting the bit to it's right, and then copying the bit to the end.)

    Thank you.


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    OK, problem.

    How do I shift all the bits in the array at once?

    I've tried this:

    *key_ptr << 1; // key_ptr points to my array

    But that only changes this:

    11110000 11001100 10101010 11110000

    to this:

    11110000 11001100 10101010 11110000

    In other words, it does nothing. (I've tried changing it to *key_ptr <<= 1; but it makes no difference.)

    Shifting an individual byte in the array works fine, but I then have to individually shift all the other bytes, which is quite complicated.

    Dos anyone have any ideas on this?

    Thank you


  • Advertisement
  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Right, I've developed a rather complicated system where I shift each byte seperately and then alter the relevant bits which should have changed due to bits coming from the byte to its right.

    Ugh.

    Complicated.


  • Closed Accounts Posts: 1,444 ✭✭✭Cantab.


    dublindude wrote: »
    Right, I've developed a rather complicated system where I shift each byte seperately and then alter the relevant bits which should have changed due to bits coming from the byte to its right.

    Ugh.

    Complicated.

    Ugh.

    Boring.


  • Registered Users, Registered Users 2 Posts: 5,401 ✭✭✭DublinDilbert


    Sounds like your trying to implement DES in "C", that's why its so inefficient in a micro-controller.... in the hardware implementations (which was what is was designed for) the shifts are just hardwired at each stage....

    As others have said you need to use assembly to do proper rotate instructions.

    In C for each rotate you'll need to store the MSB status, perform the left shift, then re-instate the LSB based on the stored value of MSB. Just pop that into a while/for loop and robert is your mothers brother!


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    Hi DublinDilbert

    Yep, that's what I'm doing.

    What complicates it is the fact that DES breaks a 56 bit key into two 28 bit halves, so when I'm doing my shifting, I have to be aware of the extra space and other things like that!

    For example, when working on the right half of the first subkey, I have to firstly shift the subkey four places to the right, and then add the missing four bits (bits 29, 30, 31, 32) from the left subkey, and then shift to the left...

    It's not amazingly difficult, but it's tricky enough and it's easy to make mistakes with all the bitmasks, etc.

    It's good fun though, and I'm learning a lot :)

    Thanks everyone for your help. I'll probably be back with more questions later on :)


  • Registered Users, Registered Users 2 Posts: 5,401 ✭✭✭DublinDilbert


    dublindude wrote: »
    Hi DublinDilbert

    Yep, that's what I'm doing.

    What complicates it is the fact that DES breaks a 56 bit key into two 28 bit halves, so when I'm doing my shifting, I have to be aware of the extra space and other things like that!

    For example, when working on the right half of the first subkey, I have to firstly shift the subkey four places to the right, and then add the missing four bits (bits 29, 30, 31, 32) from the left subkey, and then shift to the left...

    It's not amazingly difficult, but it's tricky enough and it's easy to make mistakes with all the bitmasks, etc.

    It's good fun though, and I'm learning a lot :)

    Thanks everyone for your help. I'll probably be back with more questions later on :)

    Yea the key schedule is a bitch, when you try go from the text description to the implementation....

    Here's a java script implementation (http://people.eku.edu/styere/Encrypt/JS-DES.html) which will let you check if your generating the correct round keys, it also displays the data after each round so you can debug your application.

    My advise would be to write a function that takes a 32 bit number and prints it to std-out as a 32 binary string, then you can compare the values from your code to the values from the java script implementation above, this is essential for debugging.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    dublindude wrote: »
    Hello

    I've googled this and I cannot find anything.

    If I want to perform one left shift on the following:

    10001111

    How do I make the 1 rotate to the end of the byte so it looks like:

    00011111

    Any ideas? :confused:

    Thank you.

    Quick google shows this link which has a c function to implement the rol and ror asm functions.


Advertisement