The Godfather talking
Share your stuff or I will make you regret it.
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
March 28, 2024, 05:26:13 17:26


Login with username, password and session length


Pages: [1]
Print
Author Topic: transfer the value of one variable to another using bitwise (<<)  (Read 5727 times)
0 Members and 1 Guest are viewing this topic.
Josivan
Newbie
*
Offline Offline

Posts: 14

Thank You
-Given: 0
-Receive: 1


« on: January 06, 2013, 08:36:15 20: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'. Sad
Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #1 on: January 06, 2013, 09:13:46 21: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 Offline

Posts: 14

Thank You
-Given: 0
-Receive: 1


« Reply #2 on: January 07, 2013, 12:07:10 00:07 »

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

Posts: 60

Thank You
-Given: 500
-Receive: 224


« Reply #3 on: January 07, 2013, 09:28:50 09: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
*****
 Warned
Offline Offline

Posts: 555

Thank You
-Given: 156
-Receive: 355


« Reply #4 on: January 07, 2013, 12:03:53 12: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 Offline

Posts: 833

Thank You
-Given: 312
-Receive: 392



WWW
« Reply #5 on: January 07, 2013, 03:44:32 15: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 Offline

Posts: 14

Thank You
-Given: 0
-Receive: 1


« Reply #6 on: January 11, 2013, 05:03:32 05: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

Offline Offline

Posts: 1

Thank You
-Given: 0
-Receive: 0


« Reply #7 on: January 21, 2013, 09:21:11 09: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, 11:53:56 11:53 by galove » Logged
johnf
Newbie
*
Offline Offline

Posts: 8

Thank You
-Given: 2
-Receive: 1


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

Try this in htpicc,

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

Posts: 7

Thank You
-Given: 5
-Receive: 6


« Reply #9 on: June 12, 2013, 07:59:52 07: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]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC