Can some one translate this bit of C into BASIC?

Started by Baggey, May 21, 2024, 18:24:07

Previous topic - Next topic

Baggey

( outitemp & 0x80 ? FLAG_N : 0 )


Does this mean that , If ( outitemp & 128) ie, if bit7 of outitemp is a 1 then it will be TRUE and if bit7 of outitemp is a 0 then it will be FALSE.

After the ? mark is this now a decision where we pick the value of either (FLAG_N or 0) dependinging on the TRUE or FALSE outcome?

If so which side of the : is TRUE or FALSE? ???

Or have i completely misunderstood?

Thanks in advance BAGGEY
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

dawlane

If you've dug a hole for yourself, just keep digging. You're bound to come out the other side eventually.

Baggey

#2
Quote from: dawlane on May 21, 2024, 18:39:04Read
https://en.wikipedia.org/wiki/Ternary_conditional_operator
Cool Link :D

I wish my learning curve would peterout! But it still feels exponential :o

Thankyou.
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

Baggey


Could someone have ago at this line for me

Code: BASIC
unsigned char *buffer = (unsigned char*)malloc(SizeOf(unsigned char)*lSize)


I assume im getting a pointer to :byte maybe a Tbank of sorts where the buffer size is allocated or a resize?  ::)

Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

angros47

Actually, in BASIC (which dialect? FreeBasic, for example, allows the ternary operations with the operator IIF) your second question could be easily implemented by making a string of length lSize

dawlane

#5
Quote from: Baggey on May 02, 2025, 19:17:03Could someone have ago at this line for me

Code: BASIC
unsigned char *buffer = (unsigned char*)malloc(SizeOf(unsigned char)*lSize)


I assume im getting a pointer to :byte maybe a Tbank of sorts where the buffer size is allocated or a resize?  ::)

Baggey
Read it right to left
SizeOf(unsigned char)*lSize = Deal with the SizeOf function first. Size of unsigned char, the size of char depends on the compiler. But you can assume that for a majority of systems it is a byte. So you can read it a 1*lSize.

malloc(SizeOf(unsigned char)*lSize) = Reserve a block of memory on the heap that is 1*lSize bytes.

(unsigned char*)malloc(SizeOf(unsigned char)*lSize) = The block of memory needs to be cast to that of a pointer of unsigned char. So you need to get a pointer to the previously created memory block. There are a couple of functions in the TBank to do that: Function BankBuf:Byte Ptr( bank:TBank ) and Function LockBank:Byte Ptr( bank:TBank ).

So it would translate into something like this in BlitzMax:
Local buffer:Byte Ptr = BankBuf(CreateBank(Sizeof(Byte)*lSize))

Remember that you may have to keep track of the pointer and the memory block.

Note that I have not tested this, as it was off the top of my head.

EDIT:
Const bSize:Byte = 0
Local buffer:Byte Ptr = BankBuf(CreateBank(SizeOf(bSize) * lSize))

This should be the equivalent to the C code. SizeOf(Byte) throws an error, so you will need a few variables of the size of each data type  to be used. And remember to use the correct Peek/Poke functions for the number of bytes to read and write.

Note: You may have to create the memory bank with it's own identifier to avoid a memory leak. See the advanced memory stuff in the docs.
If you've dug a hole for yourself, just keep digging. You're bound to come out the other side eventually.

Matty

The first one in "Basic" seems to be just a variant on "if..then..else" and the second one could have a variety of uses including declaring an array of bytes, or even just declaring a fixed length string variable with chars of a certain size (eg UTF could have 2 byte chars for instance).

I could be wrong and I don't know what you're using these for but that's how I saw it.

Baggey

Thankyou for the thoughts an ideas always appreciated.  ;)

QuoteRemember that you may have to keep track of the pointer and the memory block.

By the sounds of it i wern't far of. I remember doing something with the .d64 reader where id start an array at size 1 using a list. :-X And as i added to that said list, i would then have to reasign the pointers excetera. Or the info being returned was curupted.

Still that gives me an idea how to tackle it. ;D

Kind Regards Baggey
Running a PC that just Aint fast enough!? i7 4Ghz Quad core 32GB ram  2x1TB SSD and NVIDIA Quadro K1200 on 2 x HP Z24's . DID Technology stop! Or have we been assimulated!

Windows10, Parrot OS, Raspberry Pi Black Edition! , ZX Spectrum 48k, C64, Enterprise 128K, The SID chip. Im Misunderstood!

dawlane

I cannot wait till you ask about converting a union. That one would be a bit complicated with how they work.
If you've dug a hole for yourself, just keep digging. You're bound to come out the other side eventually.

PixelOutlaw

#9
I seem to remember using pointers not being exactly garbage collector friendly in BlitzMax.
My memory is a bit fuzzy but I think you have to temporarily disable the garbage in some cases.
The theory being the pointer can reference something it can then be garbage collected immediately and you've got a bad reference.

Anyone correct me if I'm wrong.
Ubuntu MATE 20.04: i5-3570K CPU @ 3.40GHz, 8GB RAM, GeForce GTX 1060 3GB

One DEFUN to rule them all, One DEFUN to find them, One DEFUN to RETURN them all, and in the darkness MULTIPLE-VALUE-BIND them.