Simplifying binary equation with << operator

Status
Not open for further replies.

Help

Advanced Member level 2
Joined
Feb 15, 2005
Messages
617
Helped
7
Reputation
14
Reaction score
3
Trophy points
1,298
Visit site
Activity points
7,065
Hi,

I have 8bits binary number. There is the pattern for the number. But how do i make a simple and more efficient equation without using "<<" shift operator in my code?

Code:
num   binary num      hex num     
0        0000 0001b     1h 
1        0000 0100b     4h
2        0001 0000b     16h
3        0100 0000b     64h

ex.
4h = 'num' .....; // how to use the 'num' value 1 to calculate out the 4hex num?

Thank for sharing..
 

Binary Equation

you can do it in assembly like this

;

SHIFTL2
movwf TEMP1
bcf CARRY
rlf TEMP1, F
rlf TEMP1, W
return


so everytime you call this subroutine
its just similar to

SHIFTL(0x01) = 0x04;

Just 4 instructions. This is asm using PIC
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Re: Binary Equation

Hi,

I'm not familiar with assembly code. If compare with your assembly code and the "<<" shift operator which one will more efficient?
I'm found that the shift operator function will take longer time to complete the instruction.

I'm using 8051. Can we use C language to do that?

Thank You.
 

Binary Equation

Your shift operator "<<" will eventually compiled to shift machine instruction. In case for pic its lsf or left shift file.

In question of efficiency, it should be adderessed to your compiler how it converts your C codes to machine codes.

Function wise, i think there is no simplier means to shift a bit other than the shift operators.

to make your C code effecient, do not make your function to take input paramaters to shift such as

SHIFT(0x01); and your function code is
unsigned short SHIFT(char TEMP){}


because, the 0x01 input parameter will be pushed to a software stack when it enters the function and will be popped once it exits teh function, Thats probably degrades the performance of your code.

Instead declare the variable as global. such as


void SHIFT() {
TEMP1=TEMP1<<2;
}



In general, functions that to be accessed very frequent, its input parameters as well as local variables must be global to improve efficiency. otherwise it will just be pushed and popped off the stack after it exits the function you called.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Binary Equation

A good optimizing C compiler can convert functions into inline code, to avoid using the stack.

Some CPUs can shift by any number of bits in one clock cycle. Other CPUs require multiple clock cycles.
 

    Help

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…