#bits

1 messages · Page 1 of 1 (latest)

gleaming lodge
#

thread time

pallid gazelle
#

Understandable

gleaming lodge
#

x << 3 left-shifts x by 3 positions.

#

1 << 3 is therefore ...1000

#

any number of zeroes to the left

#

1 << x is just 2^x

#

left-shifting by one position doubles a value

#

and right-shifting by one position halves a value

pallid gazelle
#

So x << 3 values to 00001000?

gleaming lodge
#

if x was 1, yes

pallid gazelle
#

that'd be 0*2^8+0*2^7+0*2^6+0*2^5+0*2^4+2*2^3+0*2^2+0*2^1+0*2^0
Right?

#

Which is 8

#

Because of how binary numbers work

gleaming lodge
#

you wrote 2 * 2^3

#

i presume you meant 1 * 2^3

pallid gazelle
#

Yes

#

Typo

gleaming lodge
#

but yes, that's how you'd convert that to decimal

pallid gazelle
#

It's coming back to me

#

So let's say 1 << 6.

That would value 01000000

Which would be 0*2^8+1*2^7+... Which would equal to 128?

gleaming lodge
#

1 << 6 is 2^6, which is 64

pallid gazelle
#

Why 2^6?

gleaming lodge
#

the leading position in an 8-bit number is worth 2^7, or 128

gleaming lodge
#

the last bit is 2^0, so if there are eight bits, the first bit is 2^7

pallid gazelle
#

Ah shit you're right

#

One last time

#

1 << 4

00010000

0*2^7+0*2^6+0*2^5+1*2^4+...

Which is 16?

gleaming lodge
#

Correct, 16

pallid gazelle
#

Alright, got it

#

Thanks

#

And to be clear the ">>" operator doesn't do any strange mumbo jumbo where it "right shifts" a value, right?

gleaming lodge
#

it absolutely does right-shift a value

#

2 >> 1 is 1

#

3 >> 1 is also 1

pallid gazelle
#

so basically the same as << but inverted?

gleaming lodge
#

correct

#

there is one exception: >> is sign-extending

pallid gazelle
#

Easy, intuitive

#

Thanks

gleaming lodge
#

basically, if you right shift a number with a 1 in the leftmost position, it fills the empty space with a 1

#

1111... >> 1 is still 1111...

#

A signed integer works more like this

#

-2^7 + 2^6 + 2^5 + ...

#

the leftmost bit is negative

pallid gazelle
#

WHy's that?

gleaming lodge
#

because that lets you express numbers from -128 to 127

#

It's easier to implement than if you made the leftmost bit a literal sign bit

pallid gazelle
#

-2^6 is no longer negative tho

gleaming lodge
#

like, just + or -

pallid gazelle
#

it will still be 64 since the exponent is even

gleaming lodge
#

I mean -(2^7)

pallid gazelle
#

Got it

gleaming lodge
#

the leftmost bit is just a negative value instead of a positive value

#

Without sign extension, -1 >>> 1 is like

#

a billion

pallid gazelle
#

What does the >>> represent

gleaming lodge
#

1111... shifted right 1 would be 0111....

#

which is suddenly the largest positive number

gleaming lodge
pallid gazelle
gleaming lodge
#

1111... >> 1 = 1111... = -1
1111... >>> 1 = 0111... = a billion or something

pallid gazelle
pallid gazelle
#

A little confusing to grasp but I can make sense of it in my head

gleaming lodge
pallid gazelle
#

Ah ha

#

Interesting

#

Thank you for your help :)