Is the number of bits used in a Binary field variable or is it some fixed, very large value? I’m thinking of using a byte as the only value stored in a Binary field. So is binary the type to use?
Oh, the Binary Data help page (from the Index) states:
Bytes
A byte is a collection of 8 bits. There are 256 possible combinations of 1’s and 0’s within a byte (2 multiplied by itself 8 times, i.e 2222222*2=256). These 256 combinations could represent characters, they could represent numbers from 0 to 256 or anything else.
It’s variable, but it must be a multiple of 8 bits. So it could be 0 bits, 8 bits, 16 bits, 24 bits, etc. There is no limit other than the amount of memory in your computer.
The Binary data type is similar to the Text data type, except that the Text data type requires that the data bytes must be valid UTF-8 (Unicode-8) encode text. The Binary data type makes no assumptions about what the data is, any binary value is allowed. (Because Panorama doesn’t know what type of data is in the field, it cannot display it except using hexadecimal digits.)
If the data is always going to be 1 byte, I would think it might make more sense to store it as an integer. But I can’t say for sure without knowing your application.
I’m planning to used one or more bytes whose bits represent binary flags (true/false). So a byte would do for 8 flags, but if I need more I can expand the the number of bytes to represent 8*n flags (n=# of bytes).
I think it would be easier to manipulate or test those bits, if you used integers. NOT, AND, OR, and XOR all work with integers, but not with binary data.
Exactly what I was going to say. If you used an integer, you could have up to 64 different flags. If you need more than 64 flags, probably you want to use some completely different approach, trying to work with that many flags in binary data is going to be awkward and error prone.
In fact as far as “easier to manipulate or test”, to perform those operations on binary data you would first have to convert to an integer. You cannot directly manipulate or test bits in the binary data type. In fact you really can’t do anything with binary data except convert it to another data type (either integer or text) or save it to a file. Well, I guess you can concatenate two binary data values, though I don’t think I’ve ever had an occasion to do that.
If you haven’t run across it yet, it might be useful to know that Panorama X supports the 0x___ hexadecimal format for integers. If, for example, you had your flags in a variable named Flags, and you wanted to set bit 17, you could do it like this.
Great to hear! This was something I used to teach in class, but I hadn’t thought of checking for use of hexadecimal with AND to check and OR to set in Panorama.
Obviously I’m a fan of the 0x notation, but Panorama also has functions specifically for working with individual bits. So here’s another way to set bit 17.
Flags = setbit(Flags,17,true())
If you wanted to clear bit 16, just change true to false.
Flags = setbit(Flags,17,false())
The nice thing about this technique is that you don’t have to figure out the hex value of bit 17.
If you later want to find out if the bit is set, you can do it like this:
if getbit(Flags,17)
...
... yes, bit 17 is set
...
endif
Yes, but unless it is absolutely necessary to use this flag number in multiple databases, I would strongly urge you to use a fileglobal variable, not a global. You could set it up in the .Initialize procedure like this:
right, good suggestion. However, I was wrong using 17 as an example, since every bit is a flag, so setting one flag requires me to use a power of 2, e.g. 8 (since using 7 would set the leftmost 3 bits “on”).
No. That’s what would happen if you were doing an OR operation, but setbit( calculates the appropriate power of 2, and then does the OR. Using 7 will cause it to calculate 2^6 (since it’s counting the bits from one) and then it does the OR. Setbit( never sets more than one bit at a time.
No, if you use the setbit( and getbit( function you use any number from 1 to 64. 17 is perfectly valid. The function will turn 17 into the appropriate integer that has only one bit set.