-5

I am new to C and learning how bit-wise operator works in C.I wanted to check if most significant of an address is set or not

  int main()
  {

   int addr=0x0fffffff;

   if(addr&0x80)
   {
      Printf("Bit is set");
   }
    else
     printf("Bit is not set");
     return 0;
   }

EDIT: I Would like 0x80 to be represented as 1000 0000 0000 0000 0000 0000 0000 0000 ,I have seen such implementation in past where no need of writing all the bits using some macros but not able to recall it where?

haccks
  • 104,019
  • 25
  • 176
  • 264
Amit Singh Tomar
  • 8,380
  • 27
  • 120
  • 199
  • 1
    Why do you expect it to give "Bit is not set"? `0x80` is equal to 128, and that bit is certainly set in `0x0fffffff`. – David Robinson Oct 05 '13 at 07:54
  • Why don't you just try? – marcadian Oct 05 '13 at 07:55
  • Ok, that doesn't really make any sense. Why not write `(1 << 31)` or something? – harold Oct 05 '13 at 08:23
  • You forgot a `\n` at the end of every `printf` format string (or else call `fflush`) – Basile Starynkevitch Oct 05 '13 at 08:28
  • Why SO much of downvote to this question,Isn't it too harsh for someone who is just beginner here. – Amit Singh Tomar Oct 05 '13 at 08:43
  • 4
    "I am new to C" - you've been asking questions about C on StackOverflow (hundreds of them) since Jan 2011 - maybe it's time to pick up a good book on C and read it ? – Paul R Oct 05 '13 at 08:58
  • Thanks Paul for your suggestion but do you think books will provide you all these details?? – Amit Singh Tomar Oct 05 '13 at 09:03
  • 2
    @AmitSinghTomar, take the word "book" a bit more literally and provide a bit more effort of searching and reading resources that are to your disposal? SO is really a nice site to search first, there is really a lot of detail on it. – Jens Gustedt Oct 05 '13 at 09:20
  • 3
    Also after hundrets of questions on SO you still don't know that you are supposed to post code that at least compiles? And that you might even indent it correctly such that it doesn't hurt the eyes? Voting to close. – Jens Gustedt Oct 05 '13 at 09:22
  • 2
    @AmitSinghTomar: Yes, I honestly think that if you had made the effort to read one or two good books on C you would have made a lot more progress in 3 years than with your current scattergun approach of asking random questions on StackOverflow. It's not too late though - pick a book from the [recommended list on SO](http://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) and start *reading*... – Paul R Oct 05 '13 at 10:27

4 Answers4

2

Your code int addr=0x0fffffff; and if(addr&0x80) causes this condition always true.

0x0fffffff  => 0000 1111 1111 1111 .... 1111 1111
where 0x80  =>                          1000 0000
-------------------------------------------------
bitwise AND                             1000 0000 -> non zero means true!
HirofumiTamori
  • 1,125
  • 2
  • 13
  • 27
  • Yes @Hirofumo You are right,but I was thinking is there any way to have represent 0x80 as 1000 0000 0000 0000 0000 0000 0000 0000?Some kind of short hand form. – Amit Singh Tomar Oct 05 '13 at 08:07
  • If you are interested in doing so then change `0x80` to `0x80000000` – haccks Oct 05 '13 at 08:12
  • 1
    You may caught in trap of 'endian'. Actually, the little endian machines has the value of 0x80 in 32bit form as '80 00 00 00'. However , in the same manner they also stores 0x0fffffff as 'ff ff ff 0f' :). You do not have to care endian in usual numeral/logical calculation. – HirofumiTamori Oct 05 '13 at 08:12
  • @HirofumiTamori; 'endian' ??? – haccks Oct 05 '13 at 08:13
  • Google it 'Big Endian' or 'Little Endian' – HirofumiTamori Oct 05 '13 at 08:17
  • If you want to write it as `0x80` and have that converted from little-endian to host-endian, see http://stackoverflow.com/questions/1873352/how-do-i-convert-a-value-from-host-byte-order-to-little-endian for an idea. – Joe Oct 05 '13 at 08:20
  • One concise way to write the MSB, [suggested here](http://bytes.com/topic/c/answers/497345-best-way-set-msb), is `~( (int) -1 >> 1 )`. – Joe Oct 05 '13 at 08:23
1

In c every non zero value is true. So your condition will be true.

Your condition will evaluate to 10000000b = 128 = true

Arpit
  • 12,767
  • 3
  • 27
  • 40
1

You can check if the most significant bit is set by using the mask ~(~0U >> 1). Here's some code adapted from yours:

#include <stdio.h>

int main() {
    int addr=0x0fffffff;
    unsigned int mask = ~(~0U >> 1);

    if (addr & mask) {
        printf("Bit is set");
    }
    else
        printf("Bit is not set");
    return 0;
}

Since you're new to C, the mask might look a little weird, so let's see what's happening here:

~0U is an unsigned number with every bit set to 1: the unary ~ operator negates every bit on 0, thus making every bit 1. ~0U >> 1 shifts it all by one position to the right, so now you have (on 32 bits machines):

01111111111111111111111111111111

Negating it all again, which is ~(~0U >> 1), yields:

10000000000000000000000000000000

So now you have a number with only the most significant bit set, this is used as a mask to test other numbers.

Using this kind of construct is portable because you're not relying on the size of a number. ~(~0U >> 1) will always work no matter how many bits there are in an int.

The number is declared to be unsigned because the right shift operator can cause sign extension in regular ints, and we don't want this. With unsigned numbers, right shift always inserts leading 0's.

Filipe Gonçalves
  • 20,783
  • 6
  • 53
  • 70
0

In

if(addr&0x80)  

condition addr&0x80 holds true (returns a non zero value to if) and it is equivalent to

if( a nonzero value) // any nonzero value treated as true

That's why it is printing Bit is set

haccks
  • 104,019
  • 25
  • 176
  • 264
  • 0x80 in binary would be 1000 0000 0000 0000 0000 0000 0000 0000 now checking it against 0x0fffffff would give 0 .This way I am thinking. – Amit Singh Tomar Oct 05 '13 at 08:05
  • 4
    Who told you that `0x80` would be `1000 0000 0000 0000 0000 0000 0000 0000`? – haccks Oct 05 '13 at 08:08
  • fine @haccks it is not but would like to represent 0x80 as 1000 0000 0000 0000 0000 0000 0000 0000 ,i don't want to write if my condition like if(addr&0x80000000) ,I Think I am not able to put my question in right way here. – Amit Singh Tomar Oct 05 '13 at 08:13
  • @AmitSinghTomar then write `addr < 0`, relies on two's complement and 32-bit ints so some portability-zealot will surely swear at me any minute now, but it's a safe enough assumption to make these days. – harold Oct 05 '13 at 08:15
  • And I have seen such implementation in past where no need of writing all bits.But not able to recall it where I have seen that – Amit Singh Tomar Oct 05 '13 at 08:16
  • @AmitSinghTomar; Edit your question. – haccks Oct 05 '13 at 08:17
  • @haccks I edited my question,not sure is it make a sense now. – Amit Singh Tomar Oct 05 '13 at 08:23
  • 2
    Still not clear. Give us some example what actually you want to ask. – haccks Oct 05 '13 at 08:33