❔ How Would I extract a single byte from an int

In my project, I have information which is effectively a byte[4] stored in an int32, I want to grab a specific byte from this I currently have the code
byte adjacentTileType = _tiledata >> (i * 8);
byte adjacentTileType = _tiledata >> (i * 8);
this produces an error as it should, but how would I do this properly
16 Replies
Sherbert Lemon
Sherbert LemonOP2y ago
just realised after sending that I could left shift it before right shifting it, so that all other values are overflown and elimenated is that the best way? or is there a better?
byte adjacentTileType = (byte)((_tiledata << ((i - 3) * 8)) >> (3 * 8));
byte adjacentTileType = (byte)((_tiledata << ((i - 3) * 8)) >> (3 * 8));
is what I have changed it to, but it seems a bit verbose with too many brackets also is "bitfucking" a word which people use? I have been using it a bunch with my friend when we are clueless about bit shifting, but I googled it and couldn't find anyone else using it, have we made it up? it is the verb form of "bitfuckery"
Jester
Jester2y ago
there are a few ways you could do this
Jester
Jester2y ago
int i = ...
byte a = i & byte.MaxValue;
byte b = (i >> 8) & byte.MaxValue;
etc. or
unsafe {
byte* p = &i;
byte a = p[0];
byte b = p[1];
etc
}
int i = ...
byte a = i & byte.MaxValue;
byte b = (i >> 8) & byte.MaxValue;
etc. or
unsafe {
byte* p = &i;
byte a = p[0];
byte b = p[1];
etc
}
or with this class but then you are allocating memory which is maybe not prefered https://learn.microsoft.com/en-us/dotnet/api/system.bitconverter.getbytes?view=net-7.0#system-bitconverter-getbytes(system-int32)
BitConverter.GetBytes Method (System)
Converts the specified data to an array of bytes.
Jester
Jester2y ago
more complicated bitfuckery https://graphics.stanford.edu/~seander/bithacks.html @Sherbert Lemon does this help?
Sherbert Lemon
Sherbert LemonOP2y ago
does the first one work? how does it just work with an and? but yeah defo not the getbytes, the whole point of the bitfuckery was to avoid a few hundred to a few thousand arrays needlessly oh yeah ok i get it now i'm not very good at bitfuckery, i partially did this to get better at it
Jester
Jester2y ago
the & byte.MaxValue is so only the 8 bits stay and the other turn 0
Sherbert Lemon
Sherbert LemonOP2y ago
yep that is quite a nice concise way of doing it thanks how does the pointer one work then, I will defo just use the first thing but that intrigues me
Jester
Jester2y ago
with pointers you can change the type of a thing (which is dangerous to do ofc) so if the compiler thinks it points to a byte array instead of an int. an int is 4 bytes so you can use it like its a byte array of 4 bytes instead
Sherbert Lemon
Sherbert LemonOP2y ago
ah ok that is nice, not a very c# way of doing it tho lol, and this is in unity so it is a bit of a pain to enable unsafe code thanks though, I will defo have a read through the bitfuckery page, he calls it bittwiddling, is that the more normal way of calling it?
Jester
Jester2y ago
idk
Sherbert Lemon
Sherbert LemonOP2y ago
what do you call it?
Jester
Jester2y ago
id call it bitfuckery
Jester
Jester2y ago
dont do this pointerfuck though omegalul (i just want to show it bcuz i think its a bit crazy)
Jester
Jester2y ago
pointers are dangerous so yeah go with the safe solution
Sherbert Lemon
Sherbert LemonOP2y ago
lol what, that took me a good minute to clock what it was doing, weird ig you have to pin it but is a bit of a pain to do, i'll leave pointerfuckery to cpp that is good though, I can blame you if I ever call it bitfuckery in class and say that is what everyone calls it. At least whenever we finally get to that topic lol, the computer science curriculum moves so slowly in the uk
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server