#How to do bitwise operations on shorts?

1 messages · Page 1 of 1 (latest)

undone musk
#

I want to do bitwise operations on shorts.
rn im just using the normal stuff but its actually converting them to integers and then casting back to shorts which really messes stuff up with ~ and >>>. >>> is now the same value as >> and ~ is wrong too.

split fernBOT
#

<@&987246399047479336> please have a look, thanks.

split fernBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

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.

undone musk
#

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

quaint gulch
#

Btw u can just use ints

#

Only convert to short in the end before printing

undone musk
#

No

#

That gives

quaint gulch
#

Because all arithmetic operations work the same

undone musk
#

Wrong dtuff

#

Nah

#

Like

quaint gulch
#

Well I did that and it works

undone musk
#

It doesn’t work idk why

quaint gulch
#

Which year and day was it?

undone musk
#

Right now

#

What do you mean

quaint gulch
#

The problem

#

From which advent of code year and day is it?

undone musk
#

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

quaint gulch
#

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

undone musk
#

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

gloomy plover
#

>>> is a logical right shift

undone musk
#

i know but it doesnt work

gloomy plover
#

so it ignores the sign

#

and shifts it as well

undone musk
#

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

gloomy plover
#

the right shift >>> promotes to int

undone musk
#

yes

#

but why does it make all of them 1s

gloomy plover
#

bc thats the number as int

undone musk
#

oh thats because its signed?

gloomy plover
#

11111111111111111000001101000101

undone musk
#

Ye but that’s because it’s signed

gloomy plover
#

ur -31931 starts as short, so its 1000001101000101

#

then java sees ur shift

#

and must promote the input

undone musk
#

Ye but because it’s signed it makes them zus

gloomy plover
#

((int) x ) >>> 2

undone musk
#

1

gloomy plover
#

so it becomes an int

#

11111111111111111000001101000101

undone musk
#

Ye ok but then when it casts back to short

gloomy plover
#

and then that is shifted

undone musk
#

It has an extra 1 at the sign but

gloomy plover
#

it doesnt cast back to short

#

unless u force it

#

the result of x >>> 2 is an int

#

not a short

undone musk
#

Ye I do cuz I need it as a short

gloomy plover
#

well...

#

u have to see the individual steps

undone musk
#

Because what if I’m gonna do bitwise not

gloomy plover
#

if u force it back to short, ur discarding

#

and u lose information

undone musk
#

I just want them to not promote it to an int

#

Im doing the operations on a short

gloomy plover
#

u cant

#

>>> is a lossy conversation that requires an int

#

it must promote it

undone musk
#

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 ~

gloomy plover
#

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

undone musk
#

Nah it’s for aoc

#

Im trying to process a shift right command

#

So I’m pretty sure I should shift right xd

gloomy plover
#

and its supposed to be a short?

undone musk
#

Unsigned 16 bit number

gloomy plover
#

unsigned

#

urs is signed

undone musk
#

I know

#

But stupid java doesn’t have unsigned

gloomy plover
#

well, then there is a flaw already

#

its not stupid

#

ur just doing it wrong

undone musk
#

Yes it is it needs a signed

#

So it’s not possible to cleanly do this in java

gloomy plover
#

u just dont know how to do it proper yet

undone musk
#

Then how do I do it

stiff vault
#
   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.

gloomy plover
#

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

undone musk
#

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

gloomy plover
#

yeah. so java gives u utility to handle it. but also wants to tell u "this is not nice, change it if u can"

undone musk
#

I’m just worried about what will happen when I use a bitwise not

#

And and then for example shift right

stiff vault
#

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).

undone musk
#

Ok I’ll try it

stiff vault
#

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.

undone musk
#

Ye but it changes

quaint gulch
undone musk
#

Ye I saw but it confused me cuz first you said for shifting and then for inverting

quaint gulch
#

Ye sorry mb