Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logical and Bitshift operators for vectors #2617

Open
ldci opened this issue Aug 15, 2024 · 5 comments
Open

Logical and Bitshift operators for vectors #2617

ldci opened this issue Aug 15, 2024 · 5 comments

Comments

@ldci
Copy link

ldci commented Aug 15, 2024

In Red logical operators (and, or and xor) can be used with vectors. This is not the case with R3.
Here is a general function for R3 and Red:

lbsScalar: func [
	v	[vector!]
	value 	[integer!]
	op	[integer!]
][
	_v: make vector! length? v
	case [
		op = 0 [repeat i length? v [_v/:i: v/:i % value]]	;--remainder
		op = 1 [repeat i length? v [_v/:i: v/:i and value]]	;--logical
		op = 2 [repeat i length? v [_v/:i: v/:i or value]]	;--logical
		op = 3 [repeat i length? v [_v/:i: v/:i xor value]]	;--logical 
		op = 4 [repeat i length? v [_v/:i: v/:i >> value]]	;--bitshift
		op = 5 [repeat i length? v [_v/:i: v/:i << value]]	;--bitshift
	]
	_v
]

And the samples

print "lbsScalar function test"
v: make vector! [integer! 32 4 [1 2 3 4]]
print ["v      :" v]
print ["%      :" lbsScalar v 2 0]
print ["And    :" lbsScalar v 1 1]
print ["Or     :" lbsScalar v 1 2]
print ["Xor    :" lbsScalar v 1 3]
print [">>     :" lbsScalar v 2 4]
print ["<<     :" lbsScalar v 2 5]
@ldci
Copy link
Author

ldci commented Aug 15, 2024

With R3 you can replace length? v with v/length in lbsScalar function.

@Oldes
Copy link
Owner

Oldes commented Aug 16, 2024

related: #2524

@Oldes
Copy link
Owner

Oldes commented Aug 16, 2024

@ldci your code would be more clear, if you would use words instead of integers for ops.

Also I cannot check it myself now, but in Red, does the operation modifies the vector or returns a new one? I would implement it myself by modifying it personally.

@ldci
Copy link
Author

ldci commented Aug 16, 2024 via email

@Oldes
Copy link
Owner

Oldes commented Aug 17, 2024

I'm not sure if the "always copy" is the best solution, because what if you want to modify the vector only?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants