This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

transfer the value of one variable to another using bitwise (<<)

Good morning everyone Forum
I have done a lot of programming and proficient using C but this Kile compiler is new to me and very different. it is confusing me.
I have a question and need your help.
I wonder how can I rotate the left (<<) the value of variable 'A', so to rotate, bitwise be transferred to another variable 'B'.

Parents Reply Children
  • thanks for your kind messages and help. i already know about shifts and rotates now.
    yes keil is very different. it makes code very different to what i use before. biggest change is it gives arm code and not .net code. i am very sad you cannot give a proper answer.

  • You cross posted again on the other sites. Try this. It works with all.

    
    #define BITS_TO_ROTATE 2
    bool result;
    int i;
    
    for(i= 0; i< BITS_TO_ROTATE; i++) {
        result= a&0x80;
        a<<= 1;
    
        if(result)
            a|= 0x01;
    }
    
    b= a;
    
    
    

  • It is totally irrelevant if the compiler produces x86 assembler, ARM assembler Z80 assembler or code for a virtual machine.

    The C language will still be the same. So the C code to rotate the contents of a variable and assign to another variable will look the same.

    A statement:

    a = b+1;
    


    means the same in Keil or in M$ Visual C++ (assuming you compile as C and don't compile as C++ with some fancy operator overloading).

    Same if you play with:

    a = b << 1;
    

    So the C code that performs a rotate will look the same in Keil or in Watcom or gcc or any other compiler. The exception is that different processors have different limits for size of the integer variables. So for some architectures you may implement a 64-bit rotate with just a standard "int" variable. And for some architectures you need to implement a big-number library or similar, where you need an array of smaller integer data types. But that is a completely different issue.

    Next thing is that some compilers have intrinsics or additional CRTL functions for performing left and right rotate. The normal way to check that is to read the documentation for the compiler - which I have to assume you have done.

  • thanks hannibal. i had to change the bits_to_protate to 8 and now she is working.

  • Note that for processors with barrel rotate, you can find _much_ faster ways to rotate multiple steps.

    And for processors without barrel rotate, you can still find faster solutions for 8 step rotates.

  • Mostly bit shifting is done using rotate with carry instruction (check it with your device and compiler), so rotate left 1 bit in C can be:

    b = a<<1 | CARRY;

    -ichan

  • No. Mostly shifts are not implemented using rotate instructions.

    But the processor shift instructions normally shift out bits into the carry flag, to allow multiple chained shifts for working with data larger than what fits in a single register.

    Just that most processors don't have any easy way to access that carry flag. Note that this thread is about ARM processors, not 8051 where the processor registers and lots of bits are directly accessible as byte or bit variables.

  • thanks ican. very interesting.