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
  1. Home
  2. Database & SysAdmin
  3. Database
  4. binary bit manipulation - possible?

binary bit manipulation - possible?

Scheduled Pinned Locked Moved Database
questiondatabaseannouncement
8 Posts 3 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.
  • N Offline
    N Offline
    neilsolent
    wrote on last edited by
    #1

    All, I have the following table CREATE TABLE TEST ( TEST1 binary(32) ) go The TEST1 column is a binary with 256 bits. How can I write T-SQL code to turn an arbitrary bit in TEST1 on or off, or check its state? You obviously get arithmetic overflow if you try something like: INSERT INTO TEST VALUES(0) go UPDATE TEST SET TEST1 = TEST1 | POWER(2, 137) /* Try to set bit 138 to "on" */ go "............ Arithmetic overflow occurred ......." Any ideas?

    cheers, Neil

    M E 2 Replies Last reply
    0
    • N neilsolent

      All, I have the following table CREATE TABLE TEST ( TEST1 binary(32) ) go The TEST1 column is a binary with 256 bits. How can I write T-SQL code to turn an arbitrary bit in TEST1 on or off, or check its state? You obviously get arithmetic overflow if you try something like: INSERT INTO TEST VALUES(0) go UPDATE TEST SET TEST1 = TEST1 | POWER(2, 137) /* Try to set bit 138 to "on" */ go "............ Arithmetic overflow occurred ......." Any ideas?

      cheers, Neil

      M Offline
      M Offline
      Mike Dimmick
      wrote on last edited by
      #2

      It's really hard. If you want to support yes/no type values in SQL, use a bit column. The database - well, SQL Server, at least - stores bit columns by bit manipulation in a field that's just long enough to store all the bit columns in a row. That is, if you have 8 bit columns in a particular table, they will only take one byte of space per row.

      Stability. What an interesting concept. -- Chris Maunder

      N 2 Replies Last reply
      0
      • N neilsolent

        All, I have the following table CREATE TABLE TEST ( TEST1 binary(32) ) go The TEST1 column is a binary with 256 bits. How can I write T-SQL code to turn an arbitrary bit in TEST1 on or off, or check its state? You obviously get arithmetic overflow if you try something like: INSERT INTO TEST VALUES(0) go UPDATE TEST SET TEST1 = TEST1 | POWER(2, 137) /* Try to set bit 138 to "on" */ go "............ Arithmetic overflow occurred ......." Any ideas?

        cheers, Neil

        E Offline
        E Offline
        Eric Dahlvang
        wrote on last edited by
        #3

        The int datatype is 4 bytes. CREATE TABLE BitTesting ( TestField int ) insert into BitTesting (TestField) values (0) UPDATE BitTesting SET TestField = TestField | 138 | is the bitwise OR operator.

        --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

        N 1 Reply Last reply
        0
        • M Mike Dimmick

          It's really hard. If you want to support yes/no type values in SQL, use a bit column. The database - well, SQL Server, at least - stores bit columns by bit manipulation in a field that's just long enough to store all the bit columns in a row. That is, if you have 8 bit columns in a particular table, they will only take one byte of space per row.

          Stability. What an interesting concept. -- Chris Maunder

          N Offline
          N Offline
          neilsolent
          wrote on last edited by
          #4

          Many thanks for your post. I thought of using the bit type, but then I'd have to have to 256 columns! Not ideal, and it would be hard to correlate them as well. And how would I update column 178 in code without some kind of array? I am looking into using a combination of SUBSTRING(), STUFF(), CAST(), CONVERT() and ASCII() .. I'm sure it's possible with some work! I thought someone must have done this .. don't want to reinvent the wheel..

          cheers, Neil

          1 Reply Last reply
          0
          • E Eric Dahlvang

            The int datatype is 4 bytes. CREATE TABLE BitTesting ( TestField int ) insert into BitTesting (TestField) values (0) UPDATE BitTesting SET TestField = TestField | 138 | is the bitwise OR operator.

            --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

            N Offline
            N Offline
            neilsolent
            wrote on last edited by
            #5

            Many thanks for your post. That idea is fine, but an int has only 4 bytes. That is just 32 bits ... I need a much longer array of bits (256). And I need to be able to set on/off 1 bit at a time... your example of 138 is actually turning on 3 bits: 138 = 128 + 8 + 2 = 10001010 So I am basically running into problems with large numbers....

            cheers, Neil

            E 1 Reply Last reply
            0
            • N neilsolent

              Many thanks for your post. That idea is fine, but an int has only 4 bytes. That is just 32 bits ... I need a much longer array of bits (256). And I need to be able to set on/off 1 bit at a time... your example of 138 is actually turning on 3 bits: 138 = 128 + 8 + 2 = 10001010 So I am basically running into problems with large numbers....

              cheers, Neil

              E Offline
              E Offline
              Eric Dahlvang
              wrote on last edited by
              #6

              Oh...guess I didn't read your initial post carefully enough. Maybe you could just have a separate table with the following columns: FKeyID int BitLoc tinyint BitVal bit This table could have 256 records for each record in the parent table (BitLoc would be 0 to 255). Just another idea.

              --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

              N 1 Reply Last reply
              0
              • E Eric Dahlvang

                Oh...guess I didn't read your initial post carefully enough. Maybe you could just have a separate table with the following columns: FKeyID int BitLoc tinyint BitVal bit This table could have 256 records for each record in the parent table (BitLoc would be 0 to 255). Just another idea.

                --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

                N Offline
                N Offline
                neilsolent
                wrote on last edited by
                #7

                Thanks for that. That's a neat idea, and one I hadn't thought of. It's more tables to maintain, but it would certainly work. I may just do that .. if I can't find a simple way to hack at a binary(32) directly :-)

                cheers, Neil

                1 Reply Last reply
                0
                • M Mike Dimmick

                  It's really hard. If you want to support yes/no type values in SQL, use a bit column. The database - well, SQL Server, at least - stores bit columns by bit manipulation in a field that's just long enough to store all the bit columns in a row. That is, if you have 8 bit columns in a particular table, they will only take one byte of space per row.

                  Stability. What an interesting concept. -- Chris Maunder

                  N Offline
                  N Offline
                  neilsolent
                  wrote on last edited by
                  #8

                  I have worked out how to do this, so I thought I should post the answer here in case anyone is interested: INSERT INTO TEST VALUES(0) /* Turn bit 137 on */ UPDATE TEST SET TEST1 = CONVERT(binary(32), STUFF(TEST1, 137 / 8 + 1, 1, CONVERT(binary(1), SUBSTRING(TEST1, 137 / 8 + 1, 1) | POWER(2, 137 % 8)))) /* Test bit 137 value */ SELECT SUBSTRING(TEST1, 137 / 8 + 1, 1) & POWER(2, 137 % 8) FROM TEST /* Turn bit 137 off */ UPDATE TEST SET TEST1 = CONVERT(binary(32), STUFF(TEST1, 137 / 8 + 1, 1, CONVERT(binary(1), SUBSTRING(TEST1, 137 / 8 + 1, 1) & ~POWER(2, 137 % 8))))

                  cheers, Neil

                  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