# Thread: math function to change a value 1- 8 to a bit representation

1. ## math function to change a value 1- 8 to a bit representation

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

2. ## Re: math function to change a value 1- 8 to a bit representation

see the DCD operator

3. ## Re: math function to change a value 1- 8 to a bit representation

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

4. ## Re: math function to change a value 1- 8 to a bit representation

would it be
if value > 2 then VAR.0[VALUE - 1]
cos if value below 2 would be incorrect bit ??

5. ## Re: math function to change a value 1- 8 to a bit representation

This snippet (not tested) should do the trick.

X VAR byte

X = 128
Gosub Decode
End

Decode:
If X < 1 then return
Do while X <> 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:
For i = 1 to X
Next i
Return

Cheers

Al.
Last edited by aratti; - 20th October 2014 at 09:16.

6. ## Re: math function to change a value 1- 8 to a bit representation

Hi,
Code:
```X = 8
Gosub Decode
Answer will contain the requested value
End

Decode:
For i = 1 to X
Next i
Return```
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.

7. ## Re: math function to change a value 1- 8 to a bit representation

Originally Posted by longpole001
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.

8. ## Re: math function to change a value 1- 8 to a bit representation

Just a typo! What else if not shift left.

Cheers

Al.

9. ## Re: math function to change a value 1- 8 to a bit representation

yep it sorted a few less if then statements