L
L
LamerFromSpace2020-03-01 20:38:41
go
LamerFromSpace, 2020-03-01 20:38:41

A typo in a book?

I read in the book "The Go Programming Language":

right shift of signed numbers fills the freed bits with copies of the sign bit.

Found the same fragment in the original (page 54):
right shifts of signed numbers fill the vacated bits with copies of the sign bit


In fact, the freed bits are filled with zeros, even if the sign bit is 1: https://play.golang.org/p/8NYMGJ16prw

The only difference is that when shifted by more than 8 bits, a negative signed number is always -1, while a positive one is 0.

Maybe I misunderstood something?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
falconandy, 2020-03-01
@LamerFromSpace

You are misled by %b - for negative numbers, not the "real" binary representation is displayed (instead of 11111111 -1 is displayed).
https://stackoverflow.com/questions/49573389/forma...
https://stackoverflow.com/questions/37582550/golan...
Of course -1 is 11111111 for int8, but to get this output for %b, must be explicitly converted to uint8.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question