1. Bitval Numbers

While looking through some C code, I saw bitval(10)

I know this is among bitwise functions. However is the value exactly 10? Usually there is a bit shift like 5 << 1

https://webfocusinfocenter.informationbuilders.com/wfappent/TL2s/TL_lang/source/bitval.htm - A bit string as an integer

If I was wrapping a library for Euphoria how I would go about converting it. Just give the variable the value of 10 if the C code says bitval(10)?

C  
typdef Enum myEnum 
{ 
  NONE = 0, 
  MY_VAL = BITVAL(10) 
} MyVal 

enum type myEnum 
 NONE = 0, 
 MY_VAL = 10 
 
end enum type 

Would my example be the right way of going about this?

new topic     » topic index » view message » categorize

2. Re: Bitval Numbers

Icy_Viking said...

While looking through some C code, I saw bitval(10)

I know this is among bitwise functions. However is the value exactly 10? Usually there is a bit shift like 5 << 1

https://webfocusinfocenter.informationbuilders.com/wfappent/TL2s/TL_lang/source/bitval.htm - A bit string as an integer

I don't think this is a standard function or macro in C. The function you're linking to takes four parameters, and is part of WebFOCUS Reporting Language? I found other "BITVAL" functions on Google as well, but they're for FICO Xpress, Informix, and PascalIO.

Icy_Viking said...

If I was wrapping a library for Euphoria how I would go about converting it. Just give the variable the value of 10 if the C code says bitval(10)?

C  
typdef Enum myEnum 
{ 
  NONE = 0, 
  MY_VAL = BITVAL(10) 
} MyVal 

enum type myEnum 
 NONE = 0, 
 MY_VAL = 10 
 
end enum type 

Would my example be the right way of going about this?

What you've run into here is likely a custom macro for whatever you're trying to wrap. The right way to go about this would be to track down the macro's implementation and copy it into a function.

My guess is that it's a "safe" bitshift that clamps the resulting value to some range, like valid 32-bit unsigned integers. If you could point me towards wherever you found this, I could try to track it down.

You can't use functions on enum in Euphoria, only constant. If this macro is doing anything fancy to the number then you may want to use a function to preserve the same behavior.

If the macro is something stupidly simple, like #define BITVAL( b ) ( 1 << b ) (which it might be) then you could just assemble the constant or enum values by hand.

-Greg

new topic     » goto parent     » topic index » view message » categorize

3. Re: Bitval Numbers

ghaberek said...
Icy_Viking said...

While looking through some C code, I saw bitval(10)

I know this is among bitwise functions. However is the value exactly 10? Usually there is a bit shift like 5 << 1

https://webfocusinfocenter.informationbuilders.com/wfappent/TL2s/TL_lang/source/bitval.htm - A bit string as an integer

I don't think this is a standard function or macro in C. The function you're linking to takes four parameters, and is part of WebFOCUS Reporting Language? I found other "BITVAL" functions on Google as well, but they're for FICO Xpress, Informix, and PascalIO.

Icy_Viking said...

If I was wrapping a library for Euphoria how I would go about converting it. Just give the variable the value of 10 if the C code says bitval(10)?

C  
typdef Enum myEnum 
{ 
  NONE = 0, 
  MY_VAL = BITVAL(10) 
} MyVal 

enum type myEnum 
 NONE = 0, 
 MY_VAL = 10 
 
end enum type 

Would my example be the right way of going about this?

What you've run into here is likely a custom macro for whatever you're trying to wrap. The right way to go about this would be to track down the macro's implementation and copy it into a function.

My guess is that it's a "safe" bitshift that clamps the resulting value to some range, like valid 32-bit unsigned integers. If you could point me towards wherever you found this, I could try to track it down.

You can't use functions on enum in Euphoria, only constant. If this macro is doing anything fancy to the number then you may want to use a function to preserve the same behavior.

If the macro is something stupidly simple, like #define BITVAL( b ) ( 1 << b ) (which it might be) then you could just assemble the constant or enum values by hand.

-Greg

Ah I found it, its just a #define like this

#define BITVAL(n) (1<<(n)) 

new topic     » goto parent     » topic index » view message » categorize

4. Re: Bitval Numbers

Icy_Viking said...

Ah I found it, its just a #define like this

#define BITVAL(n) (1<<(n)) 

Perfect. In these cases, assuming these are meant to be bitwise flags, I define the values as hex constants and then document the original bitwise operation next to it, like this:

public constant 
    FLAG_ONE   = #01, -- 1 << 0 
    FLAG_TWO   = #02, -- 1 << 1 
    FLAG_THREE = #04, -- 1 << 2 
    FLAG_FOUR  = #08, -- 1 << 3 
    FLAG_FIVE  = #10, -- 1 << 4 
    FLAG_SIX   = #20, -- 1 << 5 
    FLAG_SEVEN = #40, -- 1 << 7 
    FLAG_EIGHT = #80  -- 1 << 8 

-Greg

new topic     » goto parent     » topic index » view message » categorize

5. Re: Bitval Numbers

ghaberek said...
Icy_Viking said...

Ah I found it, its just a #define like this

#define BITVAL(n) (1<<(n)) 

Perfect. In these cases, assuming these are meant to be bitwise flags, I define the values as hex constants and then document the original bitwise operation next to it, like this:

public constant 
    FLAG_ONE   = #01, -- 1 << 0 
    FLAG_TWO   = #02, -- 1 << 1 
    FLAG_THREE = #04, -- 1 << 2 
    FLAG_FOUR  = #08, -- 1 << 3 
    FLAG_FIVE  = #10, -- 1 << 4 
    FLAG_SIX   = #20, -- 1 << 5 
    FLAG_SEVEN = #40, -- 1 << 7 
    FLAG_EIGHT = #80  -- 1 << 8 

-Greg

Thanks Greg! Always helpful.

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu