1. Bitshifting in Euphoria?
- Posted by Icy_Viking May 01, 2015
- 1667 views
Hello,
I am working on a re-write of my SFML2 wrapper in Euphoria. I came across this enumeration in SFML2.
typedef enum { sfNone = 0, /< No border / title bar (this flag and all others are mutually exclusive)
sfTitlebar = 1 << 0, /< Title bar + fixed border
sfResize = 1 << 1, /< Titlebar + resizable border + maximize button
sfClose = 1 << 2, /< Titlebar + close button
sfFullscreen = 1 << 3, /< Fullscreen mode (this flag and all others are mutually exclusive)
sfDefaultStyle = sfTitlebar | sfResize | sfClose /< Default window style
} sfWindowStyle;
I'm wondering if in euphoria code I'd convert it using bit shifts.
public enum type sfWindowStyle sfNone = 0, sfTitlebar = 1 -- or would I do something like bit_shift(1 < 0) ? end type
Any help would be greatly appericated.
2. Re: Bitshifting in Euphoria?
- Posted by ne1uno May 02, 2015
- 1677 views
Hello,
I am working on a re-write of my SFML2 wrapper in Euphoria. I came across this enumeration in SFML2.
typedef enum { sfNone = 0, /< No border / title bar (this flag and all others are mutually exclusive)
sfTitlebar = 1 << 0, /< Title bar + fixed border
sfResize = 1 << 1, /< Titlebar + resizable border + maximize button
sfClose = 1 << 2, /< Titlebar + close button
sfFullscreen = 1 << 3, /< Fullscreen mode (this flag and all others are mutually exclusive)
sfDefaultStyle = sfTitlebar | sfResize | sfClose /< Default window style
} sfWindowStyle;
I'm wondering if in euphoria code I'd convert it using bit shifts.
public enum type sfWindowStyle sfNone = 0, sfTitlebar = 1 -- or would I do something like bit_shift(1 < 0) ? end type
Any help would be greatly appericated.
-- sfClose = 1 << 2, ///< Titlebar + close button sfClose = shift_bits(1, -2) -- 1 << 2 Titlebar + close button
3. Re: Bitshifting in Euphoria?
- Posted by Icy_Viking May 02, 2015
- 1697 views
Hello,
I am working on a re-write of my SFML2 wrapper in Euphoria. I came across this enumeration in SFML2.
typedef enum { sfNone = 0, /< No border / title bar (this flag and all others are mutually exclusive)
sfTitlebar = 1 << 0, /< Title bar + fixed border
sfResize = 1 << 1, /< Titlebar + resizable border + maximize button
sfClose = 1 << 2, /< Titlebar + close button
sfFullscreen = 1 << 3, /< Fullscreen mode (this flag and all others are mutually exclusive)
sfDefaultStyle = sfTitlebar | sfResize | sfClose /< Default window style
} sfWindowStyle;
I'm wondering if in euphoria code I'd convert it using bit shifts.
public enum type sfWindowStyle sfNone = 0, sfTitlebar = 1 -- or would I do something like bit_shift(1 < 0) ? end type
Any help would be greatly appericated.
-- sfClose = 1 << 2, ///< Titlebar + close button sfClose = shift_bits(1, -2) -- 1 << 2 Titlebar + close button
Thanks, but could you help just a bit more, like can you explain to why shift_bits(1,-2), I kinda get it, but a explanation would be nice. So would sfTitlebar be something like sfTitlebar = shift_bits(1,1) ?
4. Re: Bitshifting in Euphoria?
- Posted by ne1uno May 02, 2015
- 1649 views
Hello,
I am working on a re-write of my SFML2 wrapper in Euphoria. I came across this enumeration in SFML2.
typedef enum { sfNone = 0, /< No border / title bar (this flag and all others are mutually exclusive)
sfTitlebar = 1 << 0, /< Title bar + fixed border
sfResize = 1 << 1, /< Titlebar + resizable border + maximize button
sfClose = 1 << 2, /< Titlebar + close button
sfFullscreen = 1 << 3, /< Fullscreen mode (this flag and all others are mutually exclusive)
sfDefaultStyle = sfTitlebar | sfResize | sfClose /< Default window style
} sfWindowStyle;
I'm wondering if in euphoria code I'd convert it using bit shifts.
public enum type sfWindowStyle sfNone = 0, sfTitlebar = 1 -- or would I do something like bit_shift(1 < 0) ? end type
Any help would be greatly appericated.
-- sfClose = 1 << 2, ///< Titlebar + close button sfClose = shift_bits(1, -2) -- 1 << 2 Titlebar + close button
Thanks, but could you help just a bit more, like can you explain to why shift_bits(1,-2), I kinda get it, but a explanation would be nice.
it helps to visualize shifted numbers in binary. shifting left by one is like multiplying by two shifting right is like dividing in half
zero is filled in on the right or the left as needed. for shift_bits() negitive numbers shift left so the constant you are creating 1 << 2 translates to the euphoria
include std/math.e --public function shift_bits(object source_number, integer shift_distance) constant x = shift_bits(1, -2) -- x = 1 << 2
1 x 00000000 0000001 x = shift_bits(1, -2) x 00000000 0000100
the euphoria help page has more details. but, not exactly a tutorial on bit manipulation.
I'm sure there are plenty of bit twiddling tutorials out there. what you are converting looks to be just a more complicated way of setting a bunch of constants to 0,1,2,4,8,16 etc pretty common way of doing this kind of thing in C headers.
So would sfTitlebar be something like sfTitlebar = shift_bits(1,1) ?
shifting by zero doesn't change the number but by convention and to keep everything looking consistent it shows left shift by zero. that could equally say sfTitlebar = 1 though C is base 0 and Euphoria is base 1 the amount of shift is actually zero in this case.
- the numbers shift_bits() works with are 32 bits that might be a bug? may need a shift_bits64()? might the docs be out of date?
5. Re: Bitshifting in Euphoria?
- Posted by petelomax May 02, 2015
- 1614 views
it helps to visualize shifted numbers in binary. shifting left by one is like multiplying by two shifting right is like dividing in half
what you are converting looks to be just a more complicated way of setting a bunch of constants to 0,1,2,4,8,16 etc pretty common way of doing this kind of thing in C headers.
That's what I would have said. The following should do it.
public enum type sfWindowStyle sfNone = 0, -- No border / title bar (this flag and all others are mutually exclusive) sfTitlebar = 1, -- Title bar + fixed border sfResize = 2, -- Titlebar + resizable border + maximize button sfClose = 4, -- Titlebar + close button sfFullscreen = 8, -- Fullscreen mode (this flag and all others are mutually exclusive) sfDefaultStyle = or_all({sfTitlebar,sfResize,sfClose}) -- Default window style end type
Pete
6. Re: Bitshifting in Euphoria?
- Posted by Icy_Viking May 02, 2015
- 1672 views
it helps to visualize shifted numbers in binary. shifting left by one is like multiplying by two shifting right is like dividing in half
what you are converting looks to be just a more complicated way of setting a bunch of constants to 0,1,2,4,8,16 etc pretty common way of doing this kind of thing in C headers.
That's what I would have said. The following should do it.
public enum type sfWindowStyle sfNone = 0, -- No border / title bar (this flag and all others are mutually exclusive) sfTitlebar = 1, -- Title bar + fixed border sfResize = 2, -- Titlebar + resizable border + maximize button sfClose = 4, -- Titlebar + close button sfFullscreen = 8, -- Fullscreen mode (this flag and all others are mutually exclusive) sfDefaultStyle = or_all({sfTitlebar,sfResize,sfClose}) -- Default window style end type
Pete
Thank you all for your help. It has been much appericated.