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#
  4. sizeof

sizeof

Scheduled Pinned Locked Moved C#
data-structures
5 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.
  • D Offline
    D Offline
    dino2094
    wrote on last edited by
    #1

    Hi, I'm trying to write a method that is passed an array and do some bit operations. I'm trying to figure out a way to determine the number of bits an element of the arrray. I could hardcode the value but I'd like the flexibility just changing the header and nothing else. int count(Uint16[] data){ //Try to do something like int bitsInCell = sizeof(data[0])*8; //Instead of int bitsInCell = 16; } I'm trying to do it the first way so if I want to sent Uint32, I don't have to search the code for 16 and switch them to 32. Thanks for your comments.

    T 1 Reply Last reply
    0
    • D dino2094

      Hi, I'm trying to write a method that is passed an array and do some bit operations. I'm trying to figure out a way to determine the number of bits an element of the arrray. I could hardcode the value but I'd like the flexibility just changing the header and nothing else. int count(Uint16[] data){ //Try to do something like int bitsInCell = sizeof(data[0])*8; //Instead of int bitsInCell = 16; } I'm trying to do it the first way so if I want to sent Uint32, I don't have to search the code for 16 and switch them to 32. Thanks for your comments.

      T Offline
      T Offline
      TJoe
      wrote on last edited by
      #2

      You can use the Marshal class, which is defined in System.Runtime.InteropServices, like so:

      int bitsInCell = Marshal.SizeOf(data[0]) * 8;

      Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

      L D 2 Replies Last reply
      0
      • T TJoe

        You can use the Marshal class, which is defined in System.Runtime.InteropServices, like so:

        int bitsInCell = Marshal.SizeOf(data[0]) * 8;

        Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Warning: Marshal.Sizeof() and sizeof() do not always agree. Try bool !

        Luc Pattyn [Forum Guidelines] [My Articles]


        this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


        T 1 Reply Last reply
        0
        • L Luc Pattyn

          Warning: Marshal.Sizeof() and sizeof() do not always agree. Try bool !

          Luc Pattyn [Forum Guidelines] [My Articles]


          this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google


          T Offline
          T Offline
          TJoe
          wrote on last edited by
          #4

          Good point. Technically, Boolean (bool) in C# maps to BOOL (int) in C++. Probably because bool is not used in the Win32 API. Plus, I believe a C++ long in a 64-bit environment will be 64 bits, but the C# int (a.k.a. Int32) is 32-bits.

          Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

          1 Reply Last reply
          0
          • T TJoe

            You can use the Marshal class, which is defined in System.Runtime.InteropServices, like so:

            int bitsInCell = Marshal.SizeOf(data[0]) * 8;

            Take care, Tom ----------------------------------------------- Check out my blog at http://tjoe.wordpress.com

            D Offline
            D Offline
            dino2094
            wrote on last edited by
            #5

            Thank you my good man!

            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