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. General Programming
  3. C / C++ / MFC
  4. memset with 32 bit values

memset with 32 bit values

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorial
4 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.
  • A Offline
    A Offline
    Alex Griffing
    wrote on last edited by
    #1

    Hi All! If I have, for example, the bits of a DIBSection and I want to set it to a given (32 bit) color, I could say: (1) for (int i=0; i<nPixels; i++) pbits[i] = mycolor; or: (2) __int32 *p=pbits; while (p<pbits+nPixels) *p++ = mycolor; but if I wanted to set it to black (or a similar color where the components are all the same) then I could just say: (3) memset(pbits, 0, nPixels*4); So I guess I'm wondering if there's a way to replace (1) and (2) by a built-in function that I don't know about but which looks something like: memset_32_at_a_time(pbits, mycolor, nPixels); (and which would presumably be faster than either of the first two methods) Thanks for any help with this!

    M B A 3 Replies Last reply
    0
    • A Alex Griffing

      Hi All! If I have, for example, the bits of a DIBSection and I want to set it to a given (32 bit) color, I could say: (1) for (int i=0; i<nPixels; i++) pbits[i] = mycolor; or: (2) __int32 *p=pbits; while (p<pbits+nPixels) *p++ = mycolor; but if I wanted to set it to black (or a similar color where the components are all the same) then I could just say: (3) memset(pbits, 0, nPixels*4); So I guess I'm wondering if there's a way to replace (1) and (2) by a built-in function that I don't know about but which looks something like: memset_32_at_a_time(pbits, mycolor, nPixels); (and which would presumably be faster than either of the first two methods) Thanks for any help with this!

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

      You might use std::fill or std::fill_n. There are no C runtime library functions for this.

      1 Reply Last reply
      0
      • A Alex Griffing

        Hi All! If I have, for example, the bits of a DIBSection and I want to set it to a given (32 bit) color, I could say: (1) for (int i=0; i<nPixels; i++) pbits[i] = mycolor; or: (2) __int32 *p=pbits; while (p<pbits+nPixels) *p++ = mycolor; but if I wanted to set it to black (or a similar color where the components are all the same) then I could just say: (3) memset(pbits, 0, nPixels*4); So I guess I'm wondering if there's a way to replace (1) and (2) by a built-in function that I don't know about but which looks something like: memset_32_at_a_time(pbits, mycolor, nPixels); (and which would presumably be faster than either of the first two methods) Thanks for any help with this!

        B Offline
        B Offline
        Ben Burnett
        wrote on last edited by
        #3

        Alex, memset does almost exactly what you do in your second function (unless you using an Alpha or PowerPC chip set, which calls RtlFillMemory). You could use memcpy in a loop, but I doubt it would give you any enhancements in speed. -Ben --------- On the topic of code with no error handling -- It's not poor coding, it's "optimistic" ;)

        1 Reply Last reply
        0
        • A Alex Griffing

          Hi All! If I have, for example, the bits of a DIBSection and I want to set it to a given (32 bit) color, I could say: (1) for (int i=0; i<nPixels; i++) pbits[i] = mycolor; or: (2) __int32 *p=pbits; while (p<pbits+nPixels) *p++ = mycolor; but if I wanted to set it to black (or a similar color where the components are all the same) then I could just say: (3) memset(pbits, 0, nPixels*4); So I guess I'm wondering if there's a way to replace (1) and (2) by a built-in function that I don't know about but which looks something like: memset_32_at_a_time(pbits, mycolor, nPixels); (and which would presumably be faster than either of the first two methods) Thanks for any help with this!

          A Offline
          A Offline
          Alex Griffing
          wrote on last edited by
          #4

          Thanks for the responses! Continuing on Ben's idea, I could manually set the values of the first scanline and then memcpy it to the other scanlines. However my original goal wasn't mainly speed gain but rather to make the code nicer, so I guess I'll give it a miss and just set the values manually :cool:

          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