Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 03, 2016, 10:53:05 22:53


Login with username, password and session length


Pages: [1]
Print
Author Topic: transfer the value of one variable to another using bitwise (<<)  (Read 3017 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, 09:36:15 21: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: 577

Thank You
-Given: 98
-Receive: 151


« Reply #1 on: January 06, 2013, 10:13:46 22: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, 01:07:10 01:07 »

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

Posts: 58

Thank You
-Given: 492
-Receive: 205


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

Posts: 556

Thank You
-Given: 156
-Receive: 354


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

Thank You
-Given: 312
-Receive: 387



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

Posts: 1

Thank You
-Given: 0
-Receive: 0


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

Posts: 8

Thank You
-Given: 2
-Receive: 1


« Reply #8 on: April 09, 2013, 02:00:40 14: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, 08:59:52 08: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