View Full Version : math function to change a value 1- 8 to a bit representation
longpole001
- 19th October 2014, 00:26
it late and minds going dull
looking for a math function to change a value 1- 8 to be bit 0 - 7 representation value
value 1 = 1 ' bit 0
2 = 2 ' bit 1
3 = 4 ' bit 2
4 = 8 ' bit 3
5 = 16 ' bit 4
6 = 32 ' bit 5
7 = 64 'bit 6
8 = 128 ' bit 7
cheers
richard
- 19th October 2014, 01:19
see the DCD operator
Amoque
- 19th October 2014, 16:21
DCD is exactly right, remember however that DCD sets all other bits to 0.
If your intent is to change only a single bit: VAR.0[VALUE - 1] = 1
longpole001
- 20th October 2014, 00:35
would it be
if value > 2 then VAR.0[VALUE - 1]
cos if value below 2 would be incorrect bit ??
aratti
- 20th October 2014, 09:05
This snippet (not tested) should do the trick.
X VAR byte
Answer VAR byte
X = 128
Gosub Decode
' variable answer should contain your bit position
End
Decode:
Answer = 0
If X < 1 then return
Do while X <> 1
Answer = answer + 1
X = X > 1
Wend
Return
Edited:
Reading again your first post I realise you need the other way around:
X = 8
Gosub Decode
Answer will contain the requested value
End
Decode:
Answer = 0
For i = 1 to X
Answer = answer < 1
Next i
Return
Cheers
Al.
HenrikOlsson
- 20th October 2014, 09:41
Hi,
X = 8
Gosub Decode
Answer will contain the requested value
End
Decode:
Answer = 0
For i = 1 to X
Answer = answer < 1
Next i
Return
Hmmm....Answer = answer < 1
If the above is meant to be a shift left operation then what you want is Answer = Answer << 1
However, since Answer is set to 0 at the entry point of the routine shifting it left any number of times will still result in it being 0 when returning.
/Henrik.
Amoque
- 20th October 2014, 13:58
would it be
if value > 2 then VAR.0[VALUE - 1]
cos if value below 2 would be incorrect bit ??
I'm a noob myself, but no... The VAR.0[] syntax is a way of addressing each bit and, while it affects the value of the byte, the bits do not interact.
Some examples:
VAR= %00000000 : VAR.0[0] = 1 : 'VAR NOW EQUALS %00000001
VAR= %00000100 : VAR.0[0] = 1 : 'VAR NOW EQUALS %00000101
VAR= %10100001 : VAR.0[1] = 1 : 'VAR NOW EQUALS %10100011
VAR= %11111100 : VAR.0[7] = 0 : 'VAR NOW EQUALS %01111101
As you can see by the last example, turning "off" bits is the same, only assign a 0. Reassigning a bit, already set (or cleared) has no effect.
aratti
- 20th October 2014, 15:45
Hmmm....Answer = answer < 1
Just a typo! What else if not shift left.
Cheers
Al.
longpole001
- 21st October 2014, 02:14
yep it sorted a few less if then statements
Powered by vBulletin® Version 4.1.7 Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.