Entire Forum This board This topic Members Entire Site
 Pages: [1]
 Author Topic: transfer the value of one variable to another using bitwise (<<)  (Read 3301 times) 0 Members and 1 Guest are viewing this topic.
Josivan
Newbie

Offline

Posts: 14

Thank You
-Given: 0

 « on: January 06, 2013, 02:36:15 14:36 »

Good morning everyone Forum
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'.
 Logged
Gallymimu
Hero Member

Offline

Posts: 664

Thank You
-Given: 148

 « Reply #1 on: January 06, 2013, 03:13:46 15:13 »

What language?  I assume C?!

Posted on: January 06, 2013, 10:05:49 22:05 - Automerged

I usually use a macro like this to do bit tests, the result can then be assigned to something else.  It's hard to provide an optimal solution without understanding what you are trying to do.

/// Macro tests bit 'bitnum' in 'integer' returns the value
/// will work with up to unsigned long long (64 bit) values
#define BIT_TST(integer,bitnum)    (0 != ((integer)&((unsigned long long)1<<(bitnum))))
 Logged
Josivan
Newbie

Offline

Posts: 14

Thank You
-Given: 0

 « Reply #2 on: January 06, 2013, 06:07:10 18:07 »

In C, since I am using MikroC to make the program.
 Logged
koseyel
Junior Member

Offline

Posts: 58

Thank You
-Given: 499

 « Reply #3 on: January 07, 2013, 03:28:50 03:28 »

Check a bit of the variable A, then set or clear the corresponding bit in the variable B.
Shift left and repeat the above operation till your are done with all bits in A (a loop is necessary).

You can find C sample code (google) on how to set, clear and check a bit in a byte (variable).

Hope that helps
Regards
 Logged
hate
Hero Member

Offline

Posts: 557

Thank You
-Given: 156

 « Reply #4 on: January 07, 2013, 06:03:53 06:03 »

Code:
#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;

Regards...
 Logged

Regards...
Ichan
Hero Member

Offline

Posts: 837

Thank You
-Given: 312

 « Reply #5 on: January 07, 2013, 09:44:32 09:44 »

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:

a = a<<1 | CARRY;

-ichan
 Logged

There is Gray, not only Black or White.
Josivan
Newbie

Offline

Posts: 14

Thank You
-Given: 0

 « Reply #6 on: January 10, 2013, 11:03:32 23:03 »

Good evening friends.
I notice that the problem was solved, I used a part of one and the other and it worked.
I adapted the idea of "hate" and it worked perfectly.

I thank you all for the help.

Code:
#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;

Regards...

 Logged
galove
Inactive

Muted
Offline

Posts: 1

Thank You
-Given: 0

 « Reply #7 on: January 21, 2013, 03:21:11 03:21 »

may,

Code:
#define BITS_TO_ROTATE 2

b=(a>>(8-BITS_TO_ROTATE))+(a<<BITS_TO_ROTATE);

do the same work for 8 bit shift operations.
 « Last Edit: January 21, 2013, 05:53:56 05:53 by galove » Logged
johnf
Newbie

Offline

Posts: 8

Thank You
-Given: 2

 « Reply #8 on: April 09, 2013, 07:00:40 07:00 »

Try this in htpicc,

Code:
destination = 1;
do {
source >>= 1;
destination >>= 1;
} while(!CARRY);
 Logged
edeca
Newbie

Offline

Posts: 7

Thank You
-Given: 5

 « Reply #9 on: June 12, 2013, 01:59:52 01:59 »

Code:
#define BITS_TO_ROTATE 2

b=(a>>(8-BITS_TO_ROTATE))+(a<<BITS_TO_ROTATE);

This is often the best way as it gives the compiler a chance of recognising it as a rotate and therefore allowing it to generate a RRF/RLF (12/16F) or RRCF/RLCF (18F) instruction.

You could also use inline ASM if the optimiser doesn't produce the correct instructions, which would guarantee each step of the rotate to execute in a single cycle.
 Logged
 Pages: [1]