#Bitwise operators
1 messages · Page 1 of 1 (latest)
lots of algorithms and optimisations use bitwise operators
so if you're a normal user you won't really get to use them
I understand that bitshifting left and right can achieve the same thing as multiplication and division by powers of 2
But I'm still a bit uncertain of things like bitwise and, or, xor etc
I'm sure there are higher level equivalents of them
for outcome but not performance *
a good example is instead of using 1 byte boolean arrays, you can use each individual bit as a boolean, since obviously a boolean only needs 1 bit to represent both false & true
is there any use in applying bitwise operators to non-primitive values or is that too unpredictable?
Only for syntactic sugars
bitwise operators are much more performant than higher level variants. there's gonna be few cases where this performance improvement comes at any sizable advantage tho
oh I see so its not that common of a practice then, I would assume
unless its absolutely performance critical
like in graphics processing and the like
yeah
someone link that evil fast square root from doom codebase
For the bitwise comparison operators such as AND and OR, how can I expect those to behave differently than logical operators (& vs && / | vs ||)
Doing a bunch of boolean operations at once is also very useful in case you have lots of those flags for a single thing: bitfields.
Say you have a bitfield like this, representing different flags that can be on or off:
mod body_feats {
const HAS_LEGS: u32 = 0b0001;
const HAS_ARMS: u32 = 0b0010;
const HAS_LONGNECK: u32 = 0b0100;
const HAS_BIGBRAIN: u32 = 0b1000;
}
```Notice each flag is a binary number with a single `1` digit and everywhere else is 0.
Say you have a variable that contains a u32, and its goal is to contain flags for body features. You can construct it like this:
```rs
let my_body_features = HAS_LEGS | HAS_ARMS | HAS_BIG_BRAIN;
```with a struct of booleans it would take more space, be more verbose, and less readable.
And then, you can even check whether it has a specific feature like this:
```rs
fn has_legs(features: u32) -> bool {
features & HAS_LEGS > 0
}
its better than some operations in my kernel (if task queue is empty then add task if its not then pause the cpu, add task, and then let the cpu continue running)
right, that uses bitwise operators
bitwise operators are most important for encoding, decoding, hashing, etc
Before the invention of YouTube comments, most people could make remarks that were slightly technically incorrect without fear of immediate public rebuke. The one exception was professors, especially if the classroom included an annoying student such as “Tom 7.” The invention of YouTube was doubly revolutionary: Now anyone can experience being a...
encoding can also just be sending over the internet or something
An entire video on bitwise float shenanigans
This helps a lot thanks
You even get the notion of a "mask", a number which is meant to "mask" out a certain flag when it has 0 or keep it when it has 1.
For example, say you want a mask that only keeps HAS_LEGS and HAS_ARMS:
// v--- keep HAS_ARMS
const MEMBERS_MASK: u32 = 0b0011;
// ^-- keep HAS_LEGS
let my_body_features = HAS_LEGS | HAS_BIG_BRAIN;
// now only has HAS_LEGS, HAS_BIG_BRAIN has been masked out
let masked_features = my_body_features & MEMBERS_MASK;
oh yeah, bitflags. reminds me that combining it with enums is one of C# features i actually miss
me too
although there's probably a crate for that
I wish people didn't need to make a whole crate just for that
and there is: bitflags
but I hate that it has to be a crate
it feels so unidiomatic
builtin typesafe bitflags would be amazing
C# is bae
C# is okay
what's bae :v
oh
C#
:( how rude of u to hit me with ur recursive answering /j
I hate OOP but I agree
Some of its concepts in isolation yeah
The problem comes when trying to make everything oop
C# will never free itself from its past. which i think is sad because i like the .net platform
it's full of like awkward things that keep reminding you that it wasn't very forward-designed
like void not being a part of the type system
bitflag enum features falling off in certain scenarios
Idk when was the last time I used pointers in C#
switch statements being stupid
Rust is bae
anxiety inducing ambiguity with operator overloads
Oh I love operator overloading
For tho things
Math and events
Curse you if you use it anywhere else
But it makes vector/matrix/quaternion math much nicer to look at
So far I think the only place I've seen it used somewhat correctly is widgets
abolish quaternions
I don't have any other example
i still haven't internalised the behaviour of equality operator in c#
like what's needed for it to stop being ReferenceEquals and start calling user defined methods
is it consistent if the compiler doesn't know the exact types?
yeahhh
thats one of the problems with operator overloading
its a reference check, unless it was operator overloaded
so it depends on the library
or if it's passed by value
oh yeah
okay you can't use == on structs without overloading it which makes sense
record structs automatically implement it for you
the differences in behaviour between value types and reference types are so annoying
I don't think they're that bad. They're just two different rulesets
not much more difficult to learn that the borrow checker, as in you need some time to get used to it
when i tried working with mainly structs in c# it felt like the standard lib and the language were fighting me
ref being a weird additional thing rather than part of the type system is bad
okay yeah ref can be confusing to use
probably an unpopular opinion: I hate var
it's a balancing thing. It's sometimes important to mention the type and other times it's obvious and you can just use var
how about when you have a generic that may work on both valuetypes and reftypes
its a consecuence of wanting to not do pointers and do pointers at the same time I guess
I started using it at one point, and then I retracted from it because I wanted to actually see the type
so probably not that unpopular
in that case you defenestrate your computer
Rider actually gives you the type if you use bar
wich is pretty amusing to me
the fact that you write var to ommit the type, then the IDE writes it as a hint thing anyways
it's quite common
omnisharp does this
rust analyser does this
global using let = System.Object
bruh
no please
don't do that
continuing with things i find annoying about c#
no streamlined way to "include dependency by pasting in code" without writing your own damn source analyser
based
no macros
bad Nebula
aren't type aliases a new thing?
wym?
oh dependencies
not imports
well guess thats an IDE problem? because the IDE could prefectly fine suggest NuGet packages
as someone who had to target .net Framework 3.5 in a slightly fucked up environment (modding) i would have found it extremely helpful to not be dependent on assembly files being there at runtime
doesn't rust compile dependencies into the binary itself. that feels like it would make modding a rust game/program very hard unless the source program had modding systems built into it
I mean you could probably just patch the binary but that's not really the biggest issue
langs like C# are relatively easy to decompile to relatively human readable code cause they compile to an intermediate language
so it's a looot easier to reverse engineer the code and write patches for it
right
i mean.
rust does technically use an IL
it just then compiles that to machine code
good example that's used often is for RGB colours
for example the number 0xFF8800 represents a kind of orange
but you might want to get the individual r, g and b values
the easiest way of doing that is bitwise operations
r = hexcode >> 16 & 0xFF
g = hexcode >> 8 & 0xFF
b = hexcode (>> 0) & 0xFF
it's just the most easy way of getting & setting bits/ranges of bits in a number
they're definitely not bad practice unless you're using them when it would be clearer to use something else. on the contrary if you go out of your way not to use them I'd say that would be bad practice because it's less clear what you're trying to do
you use << 1 when you need to multiply by 2 but don't have a multiply operation
also true
I hate it when I need to multiply by exactly 2 but dont have a multiply operation
hey, it happens
with a few <<s and add instructions you can implement fast multiplication yourself
embedded applications
in 2023?
for mere cents you have microcontrollers more powerful than most computers in the 80s
of course but they have to sacrifice some stuff
e.g. the ATTiny85 has no multiply instruction
although the SDK will probably include a software multiply algorithm and most decent C compilers will optimise *2 to <<1 anyway
well it has no hardware multiplication
this
actually nevermind the document I found was wrong, it does have mul
but it certainly doesnt have hardware div
what if you're writing assembly tho, because you hate yourself for fun?
INC $D020
JMP *-3``` hahahahaha you can't stop me
I have no clue what that means so go on
i believe it's a memory address
so it would be equivalent to the C code (*(int*)0xD020)++
yeah
my question was, if its a specific registry, or if it is just some random memory address
oh I get it sorry
without knowing what CPU this is running on it's impossible to know
it's the border color on the C64
So that program makes a rainbow border
Kinda. It flickers a lot and you can see the funny effects the video chip has on the CPU, but yes. It's Hello World in C64 ASM.
it was a stupid and distracting joke and I regret making it :)
Aw cmon do be like that with yourself