Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. BITWISE opearation

BITWISE opearation

Scheduled Pinned Locked Moved C / C++ / MFC
help
7 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Shah Satish
    wrote on last edited by
    #1

    Hello.. I want to do NOT operation..but i here output is wrong.. main() { unsigned int Value=4; /* 4 = 0000 0100 */ Value = ~ Value; /* 251 = 1111 1011 */ } but my output is 4294967291 insted of 251. Please help me out.. Thanks Shah

    J D CPalliniC C A 6 Replies Last reply
    0
    • S Shah Satish

      Hello.. I want to do NOT operation..but i here output is wrong.. main() { unsigned int Value=4; /* 4 = 0000 0100 */ Value = ~ Value; /* 251 = 1111 1011 */ } but my output is 4294967291 insted of 251. Please help me out.. Thanks Shah

      J Offline
      J Offline
      JudyL_MD
      wrote on last edited by
      #2

      If you only want to use 8 bits, you need to use unsigned char. Int is 32 bit longs, so you get FFFFFFFB when you do the NOT operation. udy

      1 Reply Last reply
      0
      • S Shah Satish

        Hello.. I want to do NOT operation..but i here output is wrong.. main() { unsigned int Value=4; /* 4 = 0000 0100 */ Value = ~ Value; /* 251 = 1111 1011 */ } but my output is 4294967291 insted of 251. Please help me out.. Thanks Shah

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        Actually The output (4294967291 = 0xFFFFFFFB) is correct (your assumptions are wrong) because the unsigned int type is 4-bytes wide:

        HEX: ~0x00000004 =
        0xFFFFFFFB

        BINARY: ~0000 0000 0000 0000 0000 0000 0000 0100 =
        1111 1111 1111 1111 1111 1111 1111 1011

        if you want to obtain 251 then use the unsigned char type:

        unsigned char Value;
        Value = 4;
        Value = ~ Value;

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        In testa che avete, signor di Ceprano?

        1 Reply Last reply
        0
        • S Shah Satish

          Hello.. I want to do NOT operation..but i here output is wrong.. main() { unsigned int Value=4; /* 4 = 0000 0100 */ Value = ~ Value; /* 251 = 1111 1011 */ } but my output is 4294967291 insted of 251. Please help me out.. Thanks Shah

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Shah Satish wrote:

          unsigned int Value=4; /* 4 = 0000 0100 */

          Actually:

          unsigned int Value = 4; /* 4 = 0000 0000 0000 0000 0000 0000 0000 0100 */
          Value = ~Value; /* 4294967291 = 1111 1111 1111 1111 1111 1111 1111 1011 */


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          • S Shah Satish

            Hello.. I want to do NOT operation..but i here output is wrong.. main() { unsigned int Value=4; /* 4 = 0000 0100 */ Value = ~ Value; /* 251 = 1111 1011 */ } but my output is 4294967291 insted of 251. Please help me out.. Thanks Shah

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            Shah Satish wrote:

            /* 4 = 0000 0100 */

            This is wrong. An integer is coded on 4 bytes, not on 1 byte, so it is 00000000 00000000 00000000 00000100 instead. Try to invert it, and you'll see that you will get 4294967291. Replace the unsigned int by an unsigned char if you want to work on 1 byte.


            Cédric Moonen Software developer
            Charting control [v1.2]

            1 Reply Last reply
            0
            • S Shah Satish

              Hello.. I want to do NOT operation..but i here output is wrong.. main() { unsigned int Value=4; /* 4 = 0000 0100 */ Value = ~ Value; /* 251 = 1111 1011 */ } but my output is 4294967291 insted of 251. Please help me out.. Thanks Shah

              A Offline
              A Offline
              apoorva_raje
              wrote on last edited by
              #6

              not very sure abt this.. bt i guess that if u wanna arrive at 251 ie. -5 (sign extended), u gotta use short int or int instead of unsigned int. hope this helps

              1 Reply Last reply
              0
              • S Shah Satish

                Hello.. I want to do NOT operation..but i here output is wrong.. main() { unsigned int Value=4; /* 4 = 0000 0100 */ Value = ~ Value; /* 251 = 1111 1011 */ } but my output is 4294967291 insted of 251. Please help me out.. Thanks Shah

                R Offline
                R Offline
                Rajkumar R
                wrote on last edited by
                #7

                Hi, You are using unsigned int it is 32 bits long for 32 bit processer, and or depending on your compiler. hence ~value is 0xFFFFFFFB not 0x000000FB, if you really want 8bits of data use unsigned char instead. Best Regards Raj

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups