Why using Variable & 255?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Why using Variable & 255?

Post by Kukulkan »

Hello,

I just found this code (doing some LibCurl here). It is a function which is used as callback for LibCurl:

Code: Select all

ProcedureC LibCurl_WriteFunction(*ptr, Size, NMemB, *Stream)
  Protected SizeProper.l  = Size & 255
  Protected NMemBProper.l = NMemB
  rdata.s=PeekS(*ptr)
  loops=CountString(rdata,#FTPEOL)
  For i=1 To loops
    file.s=RemoveString(StringField(rdata,i,#FTPEOL),Chr(13))
    file.s=RemoveString(file,Chr(10))
    PrintN(file)
  Next

  ProcedureReturn SizeProper * NMemBProper
EndProcedure
But why is the Size value in line 2 used with "and 255"? I think it is a curl size_t value because the callback function is defined like this:

Code: Select all

size_t function( char *ptr, size_t size, size_t nmemb, void *userdata);
(http://curl.haxx.se/libcurl/c/curl_easy ... TEFUNCTION)

What happens if size is something bigger 255? Or why doing it at all? Is it about signed/unsigned? But is 255 the right value (I think it works only for bytes then)?

Thanks for clarification,

Kukulkan

[EDIT] added link and better explanation [/EDIT]
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Why using Variable & 255?

Post by luis »

I don't know, but I believe you are better off writing your own from scratch.
From the doc you linked I don't understand any of the "proper" variable adjustments (size_t is unsigned long in c if I'm not mistaken).
BTW: *ptr is described as pointing to a buffer not null terminated, so PeekS is not a good choice either IMO.
"Have you tried turning it off and on again ?"
A little PureBasic review
nospam
Enthusiast
Enthusiast
Posts: 130
Joined: Mon Nov 12, 2012 9:15 am

Re: Why using Variable & 255?

Post by nospam »

luis wrote:From the doc you linked I don't understand any of the "proper" variable adjustments (size_t is unsigned long in c if I'm not mistaken).
size_t is a memory size type and requires a pointer; it has the same integer size as a void *.
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Why using Variable & 255?

Post by Kukulkan »

Hi,
size_t is a memory size type and requires a pointer; it has the same integer size as a void *.
So the "& 255" does not really make sense, right?
*ptr is described as pointing to a buffer not null terminated, so PeekS is not a good choice either IMO.
I agree, it should also specify the length and charset.

Code: Select all

rdata.s = PeekS(*ptr, Size.i * NMemB.i, #PB_UTF8)
Kukulkan
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Why using Variable & 255?

Post by luis »

@nospam

I know, it's the type of return value used for sizeof (hence the name probably), but in the prototype above it's not used to quantify a memory size, just the range a number. And it's not used with a pointer. Or so it seems from the doc.
That's the problem of having 3745 different names for the same thing, as MS likes so much.
In an ansi C compiler was defined as

typedef unsigned long size_t

don't know in more modern C++ compilers.

Kukulkan wrote: So the "& 255" does not really make sense, right?
I don't have an explanation for that. To me, no.
"Have you tried turning it off and on again ?"
A little PureBasic review
nospam
Enthusiast
Enthusiast
Posts: 130
Joined: Mon Nov 12, 2012 9:15 am

Re: Why using Variable & 255?

Post by nospam »

Kukulkan wrote:So the "& 255" does not really make sense, right?
http://www.purebasic.fr/english/viewtop ... 9&start=75

The code is full of bugs.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Why using Variable & 255?

Post by PMV »

Kukulkan wrote:What happens if size is something bigger 255? Or why doing it at all? Is it about signed/unsigned? But is 255 the right value (I think it works only for bytes then)?
PB has (in past) only signed variable-types. If you want to use
a function that returns an unsigned value with a small
variable-type (like byte), you have to use "& $FF" to
make convert the value into a unsigned value.

Code: Select all

Define Byte.b, i.i
For i = 0 To 255
  Byte = i
  Debug Byte
Next
Debug "----"
For i = 0 To 255
  Byte = i
  Debug Byte & $FF
Next
Thats the case why there are lots of codes in the forum, where
this is used ... i doesn't have looked over the function, but
as the parameter is not a byte ... it doesn't seem right.

MFG PMV
User avatar
Kukulkan
Addict
Addict
Posts: 1396
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Why using Variable & 255?

Post by Kukulkan »

PMV wrote: PB has (in past) only signed variable-types. If you want to use
a function that returns an unsigned value with a small
variable-type (like byte), you have to use "& $FF" to
make convert the value into a unsigned value.
I know about this (now using .a for an unsigned byte) but I thought there might have been another reason. So it is wrong and it should be like in my previous entry here.

Thanks all for clarification!

Kukulkan
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: Why using Variable & 255?

Post by moogle »

PMV wrote:
Kukulkan wrote:What happens if size is something bigger 255? Or why doing it at all? Is it about signed/unsigned? But is 255 the right value (I think it works only for bytes then)?
PB has (in past) only signed variable-types. If you want to use
a function that returns an unsigned value with a small
variable-type (like byte), you have to use "& $FF" to
make convert the value into a unsigned value.

Code: Select all

Define Byte.b, i.i
For i = 0 To 255
  Byte = i
  Debug Byte
Next
Debug "----"
For i = 0 To 255
  Byte = i
  Debug Byte & $FF
Next
I've always been confused about this, how do you make others show as unsigned? like long or a quad?
Image
User avatar
idle
Always Here
Always Here
Posts: 5835
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Why using Variable & 255?

Post by idle »

@moogle

signed numbers are encoded With twos compliment
so for a byte the 8th bit (leftmost) is the sign bit, it's set to 1 for negatives which gives a range of -128 to 127
the encoding is achieved With a Not + 1

so to encode -127 we have positive 127
01111111
10000000 not
10000001 + 1

To read a signed number as an unsigned number we can only masks it into a bigger signed variable eg a word , long or int
10000001 -127 is 129 unsigned but in PB we can only read this in a larger data type
If we assigned this to a word we would get
0000000001111111 127
1111111110000000 Not
1111111110000001 +1

so we mask it with $FF
1111111110000001
0000000011111111 & $FF
0000000010000001 129

a Generic procedure to convert from unsigned

Code: Select all

Procedure.q unsign(val.i) 
   
   If val > 0 
      ProcedureReturn val 
   ElseIf val > -128 
      mask = $FF  
   ElseIf val > -32768  
      mask = $FFFF 
   ElseIf val > -2147483648
     mask = $FFFFFFFF 
   EndIf     
  
   ProcedureReturn val & mask 
  
EndProcedure 
Windows 11, Manjaro, Raspberry Pi OS
Image
moogle
Enthusiast
Enthusiast
Posts: 372
Joined: Tue Feb 14, 2006 9:27 pm
Location: London, UK

Re: Why using Variable & 255?

Post by moogle »

Thanks for the explanation idle, I thought as much we couldn't display them without moving to larger variable types.
Image
Post Reply