Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3219492
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:39:09+00:00 2026-05-17T15:39:09+00:00

The following little program is very awkward using GCC version 4.2.1 (Apple Inc. build

  • 0

The following little program is very awkward using GCC version 4.2.1 (Apple Inc. build 5664) on a Mac.

#include <stdio.h>

int main(){
        int x = 1 << 32;
        int y = 32;
        int z = 1 << y;
        printf("x:%d, z: %d\n", x, z);
}

The result is x:0, z: 1.

Any idea why the values of x and z are different?

Thanks a lot.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:39:09+00:00Added an answer on May 17, 2026 at 3:39 pm

    Short answer: the Intel processor masks the shift count to 5 bits (maximum 31). In other words, the shift actually performed is 32 & 31, which is 0 (no change).

    The same result appears using gcc on a Linux 32-bit PC.

    I assembled a shorter version of this program because I was puzzled by why a left shift of 32 bits should result in a non-zero value at all:

    int main(){
        int y = 32;
        unsigned int z = 1 << y;
        unsigned int k = 1;
        k <<= y;
        printf("z: %u, k: %u\n", z, k);
    }
    

    ..using the command gcc -Wall -o a.s -S deleteme.c (comments are my own)

    main:
    leal    4(%esp), %ecx
    andl    $-16, %esp
    pushl   -4(%ecx)
    pushl   %ebp
    movl    %esp, %ebp
    pushl   %ecx
    subl    $36, %esp
    movl    $32, -16(%ebp)  ; y = 32
    movl    -16(%ebp), %ecx ; 32 in CX register
    movl    $1, %eax        ; AX = 1
    sall    %cl, %eax       ; AX <<= 32(32)
    movl    %eax, -12(%ebp) ; z = AX
    movl    $1, -8(%ebp)    ; k = 1
    movl    -16(%ebp), %ecx ; CX = y = 32
    sall    %cl, -8(%ebp)   ; k <<= CX(32)
    movl    -8(%ebp), %eax  ; AX = k
    movl    %eax, 8(%esp)
    movl    -12(%ebp), %eax
    movl    %eax, 4(%esp)
    movl    $.LC0, (%esp)
    call    printf
    addl    $36, %esp
    popl    %ecx
    popl    %ebp
    leal    -4(%ecx), %esp
    ret
    

    Ok so what does this mean? It’s this instruction that puzzles me:

    sall    %cl, -8(%ebp)   ; k <<= CX(32)
    

    Clearly k is being shifted left by 32 bits.

    You’ve got me – it’s using the sall instruction which is an arithmetic shift. I don’t know why rotating this by 32 results in the bit re-appearing in the initial position. My initial conjecture would be that the processor is optimised to perform this instruction in one clock cycle – which means that any shift by more than 31 would be regarded as a don’t care. But I’m curious to find the answer to this because I would expect that the rotate should result in all bits falling off the left end of the data type.

    I found a link to http://faydoc.tripod.com/cpu/sal.htm which explains that the shift count (in the CL register) is masked to 5 bits. This means that if you tried to shift by 32 bits the actual shift performed would be by zero bits (i.e. no change). There’s the answer!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning C++ and writing little programs as I go along. The following is
I've got the following in my .css file creating a little image next to
I'm having a little problem with the following: When I execute this line: echo
I currently use the following command, but it's a little unwieldy to type. What's
I think I'm going a little crazy. Right now, I'm working with the following
I've created the following very simple button style: <ControlTemplate x:Key=SimpleButton TargetType={x:Type ButtonBase}> <ContentPresenter x:Name=content
I am creating an installer for my program using WiX (Windows Installer XML). I
Following on from my recent question on Large, Complex Objects as a Web Service
Following my question regarding a .NET YAML Library ... as there doesn't seem to
Following this question: Good crash reporting library in c# Is there any library like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.