Ooops
January 26, 2021, 05:38:47 AM

### Author Topic: [bb] Flags by Warpy [ 1+ years ago ]  (Read 425 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Flags by Warpy [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : Flags
Author : Warpy
Posted : 1+ years ago

Description : A bit like the deathmatch flags in the Quake games... very useful

Code :
Code: BlitzBasic
1. While 1
2.         ;show instructions
3.         Print "flag 1 - man"
4.         Print "flag 2 - mouse"
5.         Print "flag 4 - blue"
6.         Print "flag 8 - red"
7.         ;user input a number, which is the values of the flags selected added together, eg
8.         num=Input("number: ")
9.
10.         ;check if flags are set
11.         If num And 1 Then Print "man!"
12.         If num And 2 Then Print "mouse!"
13.         If num And 4 Then Print "blue!"
14.         If num And 8 Then Print "red!"
15.         ;..repeat for as many powers of two as you want
16.
17.         ;this works because of the way computers store numbers.
18.         ;Numbers are stored as a string of 0s and 1s, with each power of two being represented by one of the bits in the string
19.         ;eg 1 is shown as 0001 and 2 is shown as 0010
20.         ;to make numbers other than powers of two, you just add them together
21.         ;eg 3 is 0011 (2+1) and 7 is 0111 (4+2+1)
22.
23.         ;the AND operation returns a number which is made by checking each bit in one number with the corresponding bit in another number.
24.         ;If they're both 1, Then the corresponding bit in the number returned is set To 1. Otherwise, it's set To 0
25.         ;This allows you to see whether any power of 2 is used to make a number
26.         ;and so store the values of lots of flags (True/False values) in one number
27.
28.         ;Here, the code checks for flags 1,2,4 and 8
29.         ;So, if you entered the number 9 (1001), 9 AND 1 works like this:
30.         ;1001
31.         ;1000
32.         ;returns
33.         ;1000
34.         ;Similarly, 9 AND 1 does this:
35.         ;1001
36.         ;1000
37.         ;returns
38.         ;0001
39.
40.         ;Very clever, no? :)
41. Wend

Regular K(Posted 1+ years ago)

For this to work in BMax, instead of using "And", use "&".Flags are very useful!

n8r2k(Posted 1+ years ago)

sweet a comment after 4 years, lol