Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rotate left 2 bits (like assembler)
#21
Blitz,

Sorry, I had no intentions of insulting you, just giving you the facts.

In you last solution, your answers cannot be expressed as 16 bit integers, because you are working with long integers. Don't feel bad, my personal solution had the same problem.
Examples:
1) For a num of 8192, your result is 32768, which cannot be expressed in a 16 bit integer. It should be -32768.

2) For a num of 16383, your result is 65532, which cannot be expressed in a 16 bit integer. It should be -4.

A simple fix would be to take the result from your function and store it into an integer variable before displaying it.

If you look at your results expressed in hex, they're ok.

What do you think?
*****
Reply
#22
Dude, i don't think you quite understand what bit rotation is and how numbers are stored. There is no such thing as signed or unsigned numbers. -1 and 65535 are the same number. It's just a matter of how you chose to interpret them. So what your saying is pointless. And now i'm getting a bit annoyed becuase your complaints do not make sense. It don't even need to use longs if it was compiled. But the IDE uses special function to convert numbers to help newbies. But they don't really help, they're just limiting.


32768 is in binary
1000 0000 0000 0000
Well guess what, so is -32768


65532 is in binary
1111 1111 1111 1100
Imagine the suprise, so is -4!

So you see my friend, the result of the code i posted is 100% correct. How qb choses to interpret it is another matter. If you were to run that number in x86 rol you would get the exact same binary output.

Other languages do not do such stupid assumptions. This for instance would be perfectly fine in C.

Code:
short rotateLeft ( short num )
{
    return ( (num << 2) | ((num >> 14) & 0x0003) );
}
oship me and i will give you lots of guurrls and beeea
Reply
#23
Since you insist on being so stubborn when there's no background for it, i will try it your way. Although there's absolutly no difference between the output of this and the other one binary wise.

Code:
function rotLeft2% ( num as integer )
    retl&     = ((num and &h3fff&) * 4&) or ((num and &hc000&) \ &h4000&)
    rotleft2% = (retl& and &h7fff) or -(retl& and &h8000)
end function
oship me and i will give you lots of guurrls and beeea
Reply
#24
no need to start a flame war x.x that last post was kind of a provocation...

i have to agree with blitz, its all how the number is actually interpreted, should be nothing more, nothing less to the 'argument'... bye
Reply
#25
Quote:Dude, i don't think you quite understand what bit rotation is and how numbers are stored.........
Blitz,

When you don't agree, make a stronger point. Don't attack or ridicule the other person.

BTW, I was doing bit rotation in assembly language way before you were born. That doesn't make me any smarter, but it answers your remark.

In general, your solutions have been good, except they required more complete testing.
*****
Reply
#26
here's my solution, it's a function that will rotate a number of any bit width any number of places in any direction.

rot ( n (number), a (ammount), w (width) )

if a is positive it will rotate a places to the right, if a is negative it will rotate |a| places to the left.

Code:
declare function rot ( n as integer, a as integer, w as integer ) as integer

function rot ( n as integer, a as integer, w as integer ) as integer

    dim a2 as integer, b2 as integer
    dim mask as integer
    
    a2 = 2^abs(a)
    b2 = 2^(w-abs(a))
    mask = (2^w)-1
    
    if a = 0 then
        rot = n
    elseif a > 0 then
        rot = ((n*a2) and mask) or ((n - (n and (b2-1))) / b2)
    elseif a < 0 then
        rot = ((n*b2) and mask) or ((n - (n and (a2-1))) / a2)
    end if

end function

print rot(5, 1, 3)      ' 6
print rot(5, -1, 3)     ' 3
print rot(11, 2, 4)     ' 14
print rot(11, -2, 4)    ' 14
print rot(&HFFFF, 4, 16)'65535
COUNT HACKED BY RAZVEEE

RAZVEE IS A SCRIPT KIDDIE- hacker9
Reply
#27
Quote:here's my solution, it's a function that will rotate a number of any bit width any number of places in any direction.

rot ( n (number), a (ammount), w (width) )

if a is positive it will rotate a places to the right, if a is negative it will rotate |a| places to the left.
The challenge does not ask for a general purpose shifting solution, only a shift left rotate of 2 bits.

By your definition above which says: "if a is negative it will rotate |a| places to the left", it won't perform a shift left rotate when the value of "a" is negative. Doing a shift left rotate of negative values, like -1 and -2, is part of the problem of the challenge.

Fix it and come back. But please only address a shift left rotate of 2 bits.
*****
Reply
#28
this is ridiculous... the solution is more then valid. i'd suggest to leave this topic blitz heh...
quote="NecrosIhsan"]
[Image: yagl1.png]
[/quote]
Reply
#29
Hey guys, I think we have a confusion here.

The original challenge said: "take a given 16 bit value and do a rotate left 2 bits."

In QB, a 16 bit value implies working with an INTEGER.
If we were working with a LONG, we would be working with 32 bits.

Yes, an INTEGER of -32768 has the same bit pattern as a LONG of 32768, as does an INTEGER of -4 and a LONG of 65532. BUT, both these INTEGERS have the sign bit (bit 15) set to 1, which for an INTEGER means that this is a negative number.

Therefore, if you compared an INTEGER of -32768 to a LONG of 32768, they would not be equal, because the compare is arithmetic, not bit-wise.
If you did an XOR of the INTERGER with the LONG, you would get a zero result showing that the bit patterns are equal because the leading zero bits of the LONG are ignored.

For the testing of the solutions submited, I put the code into a loop, comparing the results against proven results. So, if the submitted code produces answers expressed as LONG, they won't compare with the proven results in INTEGER, when these INTEGERS are negative. That's the problem.

So, for future solutions, please submit code that produces 16 bit values only in integers.
*****
Reply
#30
As i said before, they are the same number. It's just different way of interpreting it. In computer terms there's no such thing as signed or unsigned. However, since you seem to be sticking firmly to what you're saying i did post one that returns integers if you missed it. Now end this challenge. How long has it been going for?
oship me and i will give you lots of guurrls and beeea
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)