Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
exchange position bit

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
marcelo_bahia



Joined: 15 May 2009
Posts: 4
Location: argentine

Posted: 21 July 2009, 19:00 PM    Post subject: exchange position bit Reply with quote

I need to read individual bits in a byte and put some of those bits in a byte in different positions. you can help me , thanks
for example:

dim a as byte

dim b as byte

dim c as byte

a=bx 0001 1101 ' (a7....a0) decimal=29
b=bx 1000 1100 ' (b7....b0) decimal=140

eg I need to exchange these bits

c=000b1 b0a2a1a0 =0000 0101 īdecimal=5
Back to top
GTBecker



Joined: 18 Jan 2006
Posts: 457
Location: Cape Coral

Posted: 21 July 2009, 19:26 PM    Post subject: exchange position bit Reply with quote

Code:
c = ((b and 3) * 8) or (a and 7)



Tom
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2499
Location: Portland, OR

Posted: 21 July 2009, 19:37 PM    Post subject: Re: exchange position bit Reply with quote

GTBecker wrote:
Code:
c = ((b and 3) * 8) or (a and 7)
Or, if you prefer, use a shift operation.
Code:
c = Shl(b And 3, 3) Or (a And 7)

The compiler probably generates the same code in either case.
Back to top
twesthoff



Joined: 17 Mar 2006
Posts: 191
Location: Fredericksburg, VA

Posted: 21 July 2009, 19:39 PM    Post subject: exchange position bit Reply with quote

This should work too, same as above, but in several steps:

dim a as byte
dim b as byte
dim c as byte

a = &b0001_1101         ' (a7....a0) decimal=29
b = &b1000_1100         ' (b7....b0) decimal=140

Debug.Print "a = "; a, "b = "; b

'eg I need to exchange these bits
'c=000b1 b0a2a1a0 =0000 0101 īdecimal=5

a = a and &b0000_0111   'Mask 3 lower a-bits
b = b and &b0000_0011   'Mask 2 lower b-bits
b = b * &b0000_1000     'Adjust position of b-bits
c = a + b               'Combine into new c-bits

debug.print "c = ";c

ZBasic wrote:
Quote:
I need to read individual bits in a byte and put some of those bits in a byte in different positions. you can help me , thanks
for example:

dim a as byte

dim b as byte

dim c as byte

a=bx 0001 1101 ' (a7....a0) decimal=29
b=bx 1000 1100 ' (b7....b0) decimal=140

eg I need to exchange these bits

c=000b1 b0a2a1a0 =0000 0101 īdecimal=5




Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group