Jump to content

Question about Art of Assembly Problem (c++ question)

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Osnarf

Osnarf

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
NOTE TO MODS: I know i have the same question in two forums at once. If this is a problem please delete the one in the assembly programming forum.

The codes in C++ but its an assembly question... I got lots of views in that forum but no answers, so here it goes lol:

unsigned SetBit(unsigned BitMap, unsigned position)
{
    return BitMap | (1 << position);
}
    unsigned ClrBit(unsigned BitMap, unsigned position)
{
    return BitMap & ~(1 << position);
}

54)In code appearing in the questions above, explain what happens if the position parameter contains a value greater than or equal to the number of bits in an unsigned integer.


Unless I'm confused... in assembly you would need to put the one in a register, then shift it to the left n times, where n is the number of bits in the unsigned variable, then take the smallest register containing the set bit that you shifted, and OR it with the unsigned variable. But this would return a compile error because the operands must be the same size.

Would the compiler just extend the memory location? Or would it return a value out of range exception or a conversion error exception? Thanks for your help.

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts
Hint...what happens if you shift a 32-bit integer to the left 33 times?
sudo rm -rf /

#3
Osnarf

Osnarf

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
You'd end up will all bits unset... But you're not shifting a 32 bit integer are you? That's where I'm confused, would it just eliminate the one or would it place the overflow into another byte and keep expanding that way? I don't really understand how literals are stored

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,717 posts

Osnarf said:

You'd end up will all bits unset...
Yes

Osnarf said:

But you're not shifting a 32 bit integer are you?
Maybe. Depends on the size of an integer on the machine. Typically 32 or 64 bits, but if you get something really weird you could end up with an integer that's 36 bits. I kid you not, they exist. Old as hell, but they do (did) exist.

Osnarf said:

That's where I'm confused, would it just eliminate the one
Yes.

Osnarf said:

or would it place the overflow into another byte and keep expanding that way?
No.

Edited by dargueta, 24 March 2010 - 06:22 PM.
Fixed answer

sudo rm -rf /

#5
Osnarf

Osnarf

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
lol thanks that was driving me crazy.