Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sample program using the new _Files$ function
#21
Is there an error in this code or am I doing something wrong?

Code: (Select All)
Dim b As _Unsigned _Bit * 64
b = &B1111
Print Hex$(b)
b = &B11111111
Print Hex$(b)
b = &B111111111111
Print Hex$(b)
b = &B1111111111111111
Print Hex$(b)
it should display F, FF, FFF, and FFFF!?
Reply
#22
eoredson,

Change 64 in line one to 16. I got 'FFFF' instead of 'FFFFFFFFFFFFFFFF'. Then I changed the 16 to 32 when the limit is reached. When 32 is reached I went to 64. To keep the 16 bit 'F' in line as I go up. With 64 you was having 16 'F' instead of 4 'F'. This is not the easiest thing to grasp. With the changes I made the count goes smoothly up like it should. -GareBear.


Code: (Select All)
Dim b As _Unsigned _Bit * 16 ' This gives one more 'F'instead of twelve more in line 9.
b = &B1111
Print Hex$(b)
b = &B11111111
Print Hex$(b)
b = &B111111111111
Print Hex$(b)
b = &B1111111111111111
Print Hex$(b) ' Prints FFFF.
Dim c As _Unsigned _Bit * 32 ' I used a new variable c instead of b. for a new dimension. 32 is 32bits.
c = &B11111111111111111111
Print Hex$(c)
c = &B111111111111111111111111
Print Hex$(c)
c = &B1111111111111111111111111111
Print Hex$(c)
c = &B11111111111111111111111111111111
Print Hex$(c)
Dim d As _Unsigned _Bit * 64 ' The same as line 10. I upped it up to 64 bits
d = &B111111111111111111111111111111111111
Print Hex$(d)
d = &B1111111111111111111111111111111111111111
Print Hex$(d)
d = &B11111111111111111111111111111111111111111111
Print Hex$(d)
d = &B111111111111111111111111111111111111111111111111
Print Hex$(d)
Reply
#23
&B1111111111111111 is an unsuffixed value, so QB64 is going to try and use the same logic that QB45 used on this -- convert it to the smallest variable type that would hold this value.

Now, this value in QB45 would be -1, as it'd store those 16-bits in an integer.   Don't believe it?  Just try a simple:  PRINT &B1111111111111111

Now, once you have a value of -1, you assign that value to b, in the code:

b = &B1111111111111111
b = -1

Since b is 64 unsigned-bits, the value of -1 is &B1111111111111111111111111111111111111111111111111111111111111111, or in hex, it's the value you see on your screen: FFFFFFFFFFFFFFFF

So how to fix this, to make certain that the &B value doesn't become -1?

SUFFIX IT!!!!

Code: (Select All)
DIM b AS _UNSIGNED _BIT * 64
b = &B1111
PRINT HEX$(b)
b = &B11111111
PRINT HEX$(b)
b = &B111111111111
PRINT HEX$(b)
b = &B1111111111111111
PRINT b, HEX$(b)
b = &B1111111111111111~%
PRINT b, HEX$(b)

And that's all there is to make these type of issues go away.   If you want an unsigned integer64, then use the suffix on &B to make certain that you tell it to return you an unsigned integer64 value.  That intermediate math is what's giving you predictable, but unexpected for you, results. Wink
Reply
#24
SMcNeil,

Did I make your sense of getting to the point hurt? Smile  You showed me a short cut I did not know about. Big Grin  - GareBear.
Reply
#25
Correction to previous code, this is better and does the same thing with less. Thank you,SMcNeil!

Code: (Select All)
Dim b As _Unsigned _Bit * 64
b = &B1111
Print Hex$(b)
b = &B11111111
Print Hex$(b)
b = &B111111111111
Print Hex$(b)
b = &B1111111111111111~% ' SMcNeill use the '~%'suffix to shorten what I added - GareBear.
Print Hex$(b)
b = &B11111111111111111111~% ' Added to original by eoredson.
Print Hex$(b)
b = &B111111111111111111111111~%
Print Hex$(b)
b = &B1111111111111111111111111111~%
Print Hex$(b)
b = &B11111111111111111111111111111111~%
Print Hex$(b)
b = &B111111111111111111111111111111111111~%
Print Hex$(b)
b = &B1111111111111111111111111111111111111111~%
Print Hex$(b)
b = &B11111111111111111111111111111111111111111111~%
Print Hex$(b)
b = &B111111111111111111111111111111111111111111111111~%
Print Hex$(b)
Reply
#26
@GareBear One thing that you may want to keep in mind is to always use the proper suffix for whatever value you're sing.  

~ is UNSIGNED
%% iBYTE
% is INTEGER
& is LONG
&& is INTEGER64

So ~&& is the suffix for an unsigned integer64.

These suffixes should ALWAYS be used with literal values, if you want to make certain you keep the data types correct.   For example, 123456789098 can often get turned into a SINGLE value and become 1.23456E12, losing precision.   To make certain you retain that proper value, use the number 123456789098&& instead, to force it to be an integer64 instead.

-1 is a terrible value to mess things up.

As a byte, -1 is represented as &HFF.
As an integer, -1 is &HFFFF.
As a long, -1 is &HFFFFFFFF.
As an integer64, -1 is &HFFFFFFFFFFFFFFFF.

When using literal values, or &H, &B, &O -- which are the equivelant to literal values, just in a different base -- one should ALWAYS suffix them, or else they'll end up with unexpected results like what the programs above generate.

(Full list of suffixes can be found here: https://qb64phoenix.com/qb64wiki/index.php/TYPE )
Reply
#27
Without the SUFFIX, I got the correct result in a potentially confusing long way around. Maybe confusing with more than one variable to 'Dim' where one 'Dim' with the SUFFIX will do getting rid of the -1 and keeping the 0 and 1's unsigned. You probably saw my reasoning behind the long way around (16, 32 and 64). To me it made sense, but it is not Keep It Simple Stupid - KISS. A lot errors in coding can be spotted easier this way. Thank you for your help.
Reply
#28
Thanks steve! I rewrote the bit assignment with this and it works..

Code: (Select All)
Dim b As _Unsigned _Bit * 64
b = &B1111&
Print Hex$(b)
b = &B11111111&
Print Hex$(b)
b = &B111111111111&
Print Hex$(b)
b = &B1111111111111111&
Print Hex$(b)
Reply
#29
Just to clarify the issue I have done this:

Code: (Select All)
Dim b As _Unsigned _Bit * 64
' F - 4-bit
b = &B1111&
Print Hex$(b)
' FF - 8-bit
b = &B11111111&
Print Hex$(b)
' FFF - 12-bit
b = &B111111111111&
Print Hex$(b)
' FFFF - 16-bit
b = &B1111111111111111&
Print Hex$(b)
' FFFFFFFF - 32-bit
b = &B11111111111111111111111111111111&&
Print Hex$(b)
' FFFFFFFFFFFFFFFF - 64-bit
b = &B1111111111111111111111111111111111111111111111111111111111111111~&&
Print Hex$(b)
Reply
#30
Just to add to the confusion:

Does C have 128-bit integers?

For example, the GCC C compiler 4.6 and later has a 128-bit integer type __int128 for some architectures. GCC and compatible compilers signal the presence of 128-bit arithmetic when the macro __SIZEOF_INT128__ is defined.
Reply




Users browsing this thread: 3 Guest(s)