# negative int কেন unsigned int এর চেয়ে বড়?

 0 ``````#include int main() { unsigned int x=2; int y=-1; if(x>y) printf("x greater than y"); else printf("y greater than x"); return 0; } `````` এই প্রোগ্রামটি চালালে আউটপুট আসবে `y greater than x`। কিন্তু আমরা জানি এটি ভুুল। তাহল এই আউটপুট কেন আসে? asked 14 Mar '15, 11:03 abdullah 11●1

 0 In a 32-bit Signed Integer, the rightmost 31 bits represent the value of the Integer and the left most bit represents whether it is a positive or negative number. And in 32-bit Unsigned Integer, total 32 bits represent it's value. Now, In your code, X is an unsigned int and it's binary representation in memory will be (32-bit)- X = 00000000000000000000000000000010 And Y is a signed int, whose value is negative. So it will be stored in memory in the form of 2's complement. Like this (32-bit)- Y = 11111111111111111111111111111111 When a signed integer compared with a unsigned integer, the compiler convert the signed int to a unsigned int. So, in that case the most significant bit of Y won't represent its sign, rather it will be part of the value. So then the value of Y will be (2^32)-1 = 4294967295, which will be bigger compared to the value of X=2. That's why it is always safe to avoid comparing a signed number with an unsigned number. answered 15 Mar '15, 10:26 alavola 31●3
 toggle preview community wiki:

By Email:

Markdown Basics

• *italic* or _italic_
• **bold** or __bold__
• image?![alt text](/path/img.jpg "title")
• numbered list: 1. Foo 2. Bar
• to add a line break simply add two spaces to where you would like the new line to be.
• basic HTML tags are also supported

Question tags:

×2
×1

question asked: 14 Mar '15, 11:03

question was seen: 513 times

last updated: 25 Feb '18, 17:34