#How to do bitwise operations on shorts?
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
While you are waiting for getting help, here are some tips to improve your experience:
If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.
Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.
i havent ACTUALLY checked if ~ is wrong too btw but
its just what i am assuming
Would using a bitwise and of the max short value fix it?
Bruh I don’t get this
Because all arithmetic operations work the same
Well I did that and it works
It doesn’t work idk why
Which year and day was it?
Oh 2015
Dat 7
But I’ll show the problem
Like java is the problem
My code SHOULD work but it doesn’t because java is doing it wrong
This is what I did
For shifting u can uss 65535 - x instead of ~x
That's what inverting an unsigned short would do anyway
ok
hold on
so im shifting -31931 short >>> 2
that SHOULD give me 8401
instead it gives me -7983
which is the same value as if i would do >> 2
>>> is a logical right shift
i know but it doesnt work
yes
that is what i needs
but that doesnt happen
it gets shifted as if it was an integer
which gives 1073733841
idk why but it does
ye i really dont get why it makes all of those 1's
but it does
the right shift >>> promotes to int
bc thats the number as int
oh thats because its signed?
11111111111111111000001101000101
Ye but that’s because it’s signed
ur -31931 starts as short, so its 1000001101000101
then java sees ur shift
and must promote the input
Ye but because it’s signed it makes them zus
((int) x ) >>> 2
1
Ye ok but then when it casts back to short
and then that is shifted
It has an extra 1 at the sign but
it doesnt cast back to short
unless u force it
the result of x >>> 2 is an int
not a short
Ye I do cuz I need it as a short
Because what if I’m gonna do bitwise not
Can I use an int if I just use the short as an unsigned number anywaysv
I think that’ll mess everything up as soon as I do ~
the int has the bits on the left as 1, not 0
tbh, i feel like ur original approach is just flawed
why are u working with bits to begin with
why are u shifting
are u sure u didnt intend to use, for example an EnumSet
(bit masks)
or similar
Nah it’s for aoc
Im trying to process a shift right command
So I’m pretty sure I should shift right xd
and its supposed to be a short?
Unsigned 16 bit number
u just dont know how to do it proper yet
Then how do I do it
short a = -31931;
short b = (short) (Short.toUnsignedInt(a) >>> 2);
Oak had unsigned types... They were removed 'to simplify the language'. The end result is this hoop-jumping.
yeah. u just go one type higher, so that u have place for the full unsigned range.
and to not misinterpret negatives in that process, u gotta use that helper method
but the actual issue is one level deeper
unsigned types are usually considered to be flawed to begin with
u wouldnt use them in proper c/c++ code
the pain comes mostly when u have to interact with old or inproper code that still uses them
ofc this is up to debate, but thats the gist of it
Ye I mean… rn I’m kind of forced to
But do I need the short conversion can’t I just use an int to begin with
yeah. so java gives u utility to handle it. but also wants to tell u "this is not nice, change it if u can"
I’m just worried about what will happen when I use a bitwise not
And and then for example shift right
Unsigned types have their place. Java is just a little clumsy at working with them (and forces you to work at one scale higher than the problem should require).
Ok I’ll try it
Before Java 8 we would have to mask off ourselves (what that helper does)
short a = -31931;
int i = ((1 << 16) - 1) & (int) a;
short b = (short) (i >> 2);
Though i >> 2 is really just i / 4, JIT will easily spot that optimisation.
Ye but it changes
I told u earlier what u can do
Ye I saw but it confused me cuz first you said for shifting and then for inverting
Ye sorry mb