Using bytes in a binary field

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.

I assume for unsigned values, the range is 0-255.

If you are talking about the byte( function, it will return the least significant byte of any number between -254 and 255.

If the binaryvalue( function is given a single byte of binary data, it will treat it as unsigned.

The number of bits used by a cell in a binary field varies. It could be as few as 8. It could be enormous.

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.

Correct. I’ve submitted a correction for this.

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).

Thanks for the responses.

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.

Very helpful information from Dave and Jim. 64 flags is more then enough.

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.

Flags = Flags or 0x20000

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.

This lets me set/check multiple bits as well!

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

From the help for setbit(
————-
Parameters

This function has three parameters:

number – the initial integer value.

bit – the bit to flip, from 1 to 64.
————

Dave’s hexadecimal example above implies bit 0 on low end.
Is it really 1 to 64, or 0 to 63?

It’s 1 to 64. If you look at the examples, you can see which bits are being set.

I had forgotten about the bit(, setbit(, and getbit( functions. I just assumed the zero convention would be the one you were familiar with.

So, I could even use a (constant) global variable, properly named, set to 17 and use something like setbit(Flags, IsOverdue, true())

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:

letfileglobal isOverdue = 17

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.