#bits
1 messages · Page 1 of 1 (latest)
Understandable
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
Undertood
So x << 3 values to 00001000?
if x was 1, yes
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
but yes, that's how you'd convert that to decimal
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?
1 << 6 is 2^6, which is 64
Why 2^6?
the leading position in an 8-bit number is worth 2^7, or 128
you've written out nine bits here
the last bit is 2^0, so if there are eight bits, the first bit is 2^7
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?
Correct, 16
Alright, got it
Thanks
And to be clear the ">>" operator doesn't do any strange mumbo jumbo where it "right shifts" a value, right?
so basically the same as << but inverted?
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
WHy's that?
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
-2^6 is no longer negative tho
like, just + or -
it will still be 64 since the exponent is even
well yeah, the rest of the bits are interpreted as positive values
I mean -(2^7)
Got it
the leftmost bit is just a negative value instead of a positive value
Without sign extension, -1 >>> 1 is like
a billion
What does the >>> represent
1111... shifted right 1 would be 0111....
which is suddenly the largest positive number
That's the unsigned right-shift operator. It doesn't do sign-extension, so it always fills the missing bits in with zeroes
There is no largest number. I'm guessing you mean the integer limit of your computer, right?
1111... >> 1 = 1111... = -1
1111... >>> 1 = 0111... = a billion or something
Like if it were 32 bit it'd be 2 billion something something
Makes sense
A little confusing to grasp but I can make sense of it in my head
the largest signed 32-bit integer, yes (: