Unsigned addition really performs subtraction.

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
wireboy5
Posts: 4
Joined: Sun Apr 04, 2021 7:51 pm

Unsigned addition really performs subtraction.

Post by wireboy5 »

I am working on finding out the physical address range that I need to add to my page tables to access the provided framebuffer.

When I add the size of the framebuffer (Which I calculated as pitch * height) to the address of the framebuffer, the result appears to be a subtraction instead.

The calculated size of my framebuffer is 0x384000 bytes, and GRUB says that the start address is at 0x40000000. However, when I add these two numbers, I get 0x3fc7c000.

My first thought was that I was (somehow) getting a negative number for the size of my framebuffer (Or, at least the sign bit was set because of the multiplication result being large enough.) however, both my integers are `unsigned long long`and `unsigned long`, and I even checked with (x & (1 << 63)) and the sign bits are not set on either. Even if the sign bits are set, they should not be added as a signed integer.

What do you guys think is going on here? Am I doing something wrong?
This is the github repo: https://github.com/wireboy5/64bitOS

Thank's for your consideration!
Octocontrabass
Member
Member
Posts: 5563
Joined: Mon Mar 25, 2013 7:01 pm

Re: Unsigned addition really performs subtraction.

Post by Octocontrabass »

You're converting the number to signed int before printing it.
alexfru
Member
Member
Posts: 1111
Joined: Tue Mar 04, 2014 5:27 am

Re: Unsigned addition really performs subtraction.

Post by alexfru »

wireboy5 wrote:1 << 63
Also, you likely want 1ULL << 63.
wireboy5
Posts: 4
Joined: Sun Apr 04, 2021 7:51 pm

Re: Unsigned addition really performs subtraction.

Post by wireboy5 »

Thank you guys!

That was probably the silliest mistake I have made in a while. At least I have learned something though: Always check your debugging methods.

Thanks again!
Post Reply