Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

90
Visualizações
bit shifting with unsigned long type produces wrong results

I'm a bit confused because I wanted to initialize a variable of type unsigned long whose size is 8 bytes on my system (on every modern system I suppose). When I want to assign 1 << 63 to the variable, I get a compiler warning however and the number is in fact 0. When I do 1 << 30 I get the expected result of 2 ^ 30 = 1073741824. Yet when I do 1 << 31, I get the result of 2 ^ 64 (I think; actually this shouldn't be possible) which prints 18446744071562067968.

Can anyone explain this behaviour to me?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

1 << 63 will be computed in int arithmetic, and your int is probably 32 bit.

Remedy this by promoting one of the arguments: 1ULL << 63 will do it.

ULL means the expression will be at least 64 bits.

over 4 years ago · Santiago Trujillo Relatório

0

The expression 1 << 63 has type int. The range of an int is -231 … 231 - 1 on most systems, 263 is too large for that. Try either (unsigned long)1 << 63 or 1UL << 63 to shift a value of type unsigned long left by 63 places.

over 4 years ago · Santiago Trujillo Relatório

0

The 1 here, is called an integer constant. By the norms specified in the standard, C11, chapter §6.4.4.1 the syntax for the same is

integer-constant:
     decimal-constant integer-suffixopt
     octal-constant integer-suffixopt
      hexadecimal-constant integer-suffixopt

and regarding the Semantics,

The type of an integer constant is the first of the corresponding list in which its value can be represented.

and the table says, if there is no suffix, and the value is representable in the int range it should be considered as int. So, 1 here, is considered an int, which is of generally 4 bytes, or 32 bits, also same in your case.

To explicitly specify the 1 as unsigned long (64) bit, we can use the suffix, like

1UL << 63

should solve your issue.

Please note: unsigned long is not guaranteed to be of 64 bits. unsigned long long is guaranteed to have at least 64 bits. However, as long as you're using the platform where unsigned long is of 64 bits , you should be fine

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda