Select + Case + Default + EndSelect

Just starting out? Need help? Post your questions and find answers here.
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Select + Case + Default + EndSelect

Post by 4RESTER »

What wrong in this code?

Code: Select all

Global BPB_Media.B = $F8

Select BPB_Media.B
    Case $F0,$F8 To $FF
        Debug "BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0") 
    Default
        Debug "Wrong BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
EndSelect
In fact, with such value
BPB_Media.B = $F8
the branch of Default shall not be executed.
See ya... Image
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Select + Case + Default + EndSelect

Post by gnasen »

no, its correct, because in PB most variables are signed:
Byte: -128 to +127
So if you store $F8 = 248 in a byte, you get: -8. Use this instead:
Char (= unsigned Byte): 0 to 255

Code: Select all

Global BPB_Media.c = $F8

Select BPB_Media.c
  Case $F0,$F8 To $FF
    Debug "BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
  Default
    Debug "Wrong BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
EndSelect
pb 5.11
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Select + Case + Default + EndSelect

Post by netmaestro »

.c is 16 bits in unicode mode, better use .a for unsigned byte.
BERESHEIT
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: Select + Case + Default + EndSelect

Post by 4RESTER »

gnasen wrote:no, its correct, because in PB most variables are signed:
Byte: -128 to +127
So if you store $F8 = 248 in a byte, you get: -8. Use this instead:

Code: Select all

Global BPB_Media.c = $F8

Select BPB_Media.c
  Case $F0,$F8 To $FF
    Debug "BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
  Default
    Debug "Wrong BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
EndSelect
Hmmm, very strange behavior of the compiler.
So literal $F8 must be also be as -8 :-)
Last edited by 4RESTER on Sun May 15, 2011 12:12 am, edited 1 time in total.
See ya... Image
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Select + Case + Default + EndSelect

Post by Demivec »

4RESTER wrote:What wrong in this code?

Code: Select all

Global BPB_Media.B = $F8

Select BPB_Media.B
    Case $F0,$F8 To $FF
        Debug "BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0") 
    Default
        Debug "Wrong BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
EndSelect
In fact, with such value
BPB_Media.B = $F8
the branch of Default shall not be executed.
Same as the posting in your other thread. When the hexadecimal literals are converted to decimal it would read this way:

Code: Select all

Global BPB_Media.B = -8 ;$F8

Select BPB_Media.B
  Case 240, 248 To 255 ;$F0,$F8 To $FF
    Debug "BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0") 
  Default
    Debug "Wrong BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
EndSelect
Which clearly shows the default case will be executed.

Use this code to prove it to yourself:

Code: Select all

Global BPB_Media.B = $F8

Select BPB_Media.B
  Case $F0,$F8 To $FF
    Debug "BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0") 
  Case -8
    Debug "Right BPB_Media: " + Str(BPB_Media)
  Default
    Debug "Wrong BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
EndSelect
gnasen
Enthusiast
Enthusiast
Posts: 282
Joined: Wed Sep 24, 2008 12:21 am

Re: Select + Case + Default + EndSelect

Post by gnasen »

netmaestro wrote:.c is 16 bits in unicode mode, better use .a for unsigned byte.
Ah thanks, I missed that. I never used Unicode until now :oops:
pb 5.11
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: Select + Case + Default + EndSelect

Post by 4RESTER »

Demivec wrote:
4RESTER wrote:What wrong in this code?

Code: Select all

Global BPB_Media.B = $F8

Select BPB_Media.B
    Case $F0,$F8 To $FF
        Debug "BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0") 
    Default
        Debug "Wrong BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
EndSelect
In fact, with such value
BPB_Media.B = $F8
the branch of Default shall not be executed.
Same as the posting in your other thread. When the hexadecimal literals are converted to decimal it would read this way:

Code: Select all

Global BPB_Media.B = -8 ;$F8

Select BPB_Media.B
  Case 240, 248 To 255 ;$F0,$F8 To $FF
    Debug "BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0") 
  Default
    Debug "Wrong BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
EndSelect
Which clearly shows the default case will be executed.

Use this code to prove it to yourself:

Code: Select all

Global BPB_Media.B = $F8

Select BPB_Media.B
  Case $F0,$F8 To $FF
    Debug "BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0") 
  Case -8
    Debug "Right BPB_Media: " + Str(BPB_Media)
  Default
    Debug "Wrong BPB_Media: $"+RSet(Hex(BPB_Media, #PB_Byte),2,"0")
EndSelect
Looks like this dialect of BASIC is a finding for CodeTalkers (NavajoBasic).
See ya... Image
User avatar
4RESTER
User
User
Posts: 63
Joined: Thu Aug 19, 2010 11:03 pm
Location: Uman, Ukraine

Re: Select + Case + Default + EndSelect

Post by 4RESTER »

netmaestro wrote:.c is 16 bits in unicode mode, better use .a for unsigned byte.
Okay.
8-bits unsigned as .A
16-bits unsigned as .U

What about 32-bits UNSIGNED values?
See ya... Image
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Select + Case + Default + EndSelect

Post by luis »

They have been requested (see in the appropriate subforum) but the official answer for the team, if I recall correctly (please correct me if I'm wrong), is the .a and .u were added as a convenience for working with ascii/unicode, and no larger unsigned data type are planned.

Obviously is possible to work with signed integers just the same, but I would like a 32 byte unsigned type too, for what it's worth. Especially to work with c/c++ libraries in a simpler way.

edit: found the post I was mentioning
http://www.purebasic.fr/english/viewtop ... 80#p295680
"Have you tried turning it off and on again ?"
A little PureBasic review
Post Reply