Off Topic
    • Categories
    • Tags
    • Users
    • Groups
    • Amazon
    • OT Fundraiser
    • Register
    • Login

    The OFFICIAL programming thread

    Tech
    22
    1.4k
    10.4k
    Loading More Posts
    • 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 Former User
      last edited by A Former User

      My Dad tried to get me interested in machine code / assembly programming when I was grade school. He’d bought this thing called the Microprofessor and was happily learning about how setting bits in code translated directly to the hardware.

      d3eb8fa3-489b-4798-aab5-306b9818c65c-image.png

      It wasn’t too long before he was building custom hardware and programming eeproms to control lighting for productions at the theatre where he was the Technical Director.

      Anyway, I listened dutifully to every he excitedly tried to teach me on the subject but really had zero interest in it myself. However, just now, 4 decades later, I had a requirement to store cartesian coordinates as a single unsigned integer and boy I’m glad I understood enough of what he taught me to not be put off by things like two’s-complement and bit shifting/masking.

      const BITS_PER_DIMENSION = 2
      const MAX_EXTENT = 1 << (BITS_PER_DIMENSION - 1)
      
      func _ready():
      	print("Bits per dimension: %d, Max extent: %d" % [BITS_PER_DIMENSION, MAX_EXTENT])
      	var ids = []
      	for col in range(-MAX_EXTENT, MAX_EXTENT):
      		var col_normalized = col + MAX_EXTENT
      		var col_shifted = col_normalized << BITS_PER_DIMENSION
      		for row in range(-MAX_EXTENT, MAX_EXTENT):
      			var row_normalized = row + MAX_EXTENT
      			var id = col_shifted + row_normalized
      			ids.push_back(id)
      			print("col: %4d, row: %4d, id: %4d" % [col, row, id])
      	
      	print("**************** Reversed **************")
              var mask_high = (1 << BITS_PER_DIMENSION) - 1
      	print("mask high: %d" % mask_high)
      	for id in ids:
      		var row_normalized = id & mask_high
      		var row = row_normalized - MAX_EXTENT
      		var col_normalized = id >> BITS_PER_DIMENSION
      		var col = col_normalized - MAX_EXTENT
      		print("col: %4d, row: %4d, id: %4d" % [col, row, id])
      

      Result:

      --- Debugging process started ---
      Godot Engine v3.3.2.stable.official - https://godotengine.org
      OpenGL ES 2.0 Renderer: GeForce 940MX/PCIe/SSE2
      OpenGL ES Batching: ON
       
      Bits per dimension: 2, Max extent: 2
      col:   -2, row:   -2, id:    0
      col:   -2, row:   -1, id:    1
      col:   -2, row:    0, id:    2
      col:   -2, row:    1, id:    3
      col:   -1, row:   -2, id:    4
      col:   -1, row:   -1, id:    5
      col:   -1, row:    0, id:    6
      col:   -1, row:    1, id:    7
      col:    0, row:   -2, id:    8
      col:    0, row:   -1, id:    9
      col:    0, row:    0, id:   10
      col:    0, row:    1, id:   11
      col:    1, row:   -2, id:   12
      col:    1, row:   -1, id:   13
      col:    1, row:    0, id:   14
      col:    1, row:    1, id:   15
      **************** Reversed **************
      mask high: 3
      col:   -2, row:   -2, id:    0
      col:   -2, row:   -1, id:    1
      col:   -2, row:    0, id:    2
      col:   -2, row:    1, id:    3
      col:   -1, row:   -2, id:    4
      col:   -1, row:   -1, id:    5
      col:   -1, row:    0, id:    6
      col:   -1, row:    1, id:    7
      col:    0, row:   -2, id:    8
      col:    0, row:   -1, id:    9
      col:    0, row:    0, id:   10
      col:    0, row:    1, id:   11
      col:    1, row:   -2, id:   12
      col:    1, row:   -1, id:   13
      col:    1, row:    0, id:   14
      col:    1, row:    1, id:   15
      
      Gators1G rote7R 2 Replies Last reply Reply Quote 3
      • Gators1G
        Gators1 @A Former User
        last edited by

        @hog said in The OFFICIAL programming thread:

        My Dad tried to get me interested in machine code / assembly programming when I was grade school. He’d bought this thing called the Microprofessor and was happily learning about how setting bits in code translated directly to the hardware.

        d3eb8fa3-489b-4798-aab5-306b9818c65c-image.png

        It wasn’t too long before he was building custom hardware and programming eeproms to control lighting for productions at the theatre where he was the Technical Director.

        Anyway, I listened dutifully to every he excitedly tried to teach me on the subject but really had zero interest in it myself. However, just now, 4 decades later, I had a requirement to store cartesian coordinates as a single unsigned integer and boy I’m glad I understood enough of what he taught me to not be put off by things like two’s-complement and bit shifting.

        const BITS_PER_DIMENSION = 2
        const MAX_EXTENT = 1 << (BITS_PER_DIMENSION - 1)
        
        func _ready():
        	print("Bits per dimension: %d, Max extent: %d" % [BITS_PER_DIMENSION, MAX_EXTENT])
        	var ids = []
        	for col in range(-MAX_EXTENT, MAX_EXTENT):
        		var col_normalized = col + MAX_EXTENT
        		var col_shifted = col_normalized << BITS_PER_DIMENSION
        		for row in range(-MAX_EXTENT, MAX_EXTENT):
        			var row_normalized = row + MAX_EXTENT
        			var id = col_shifted + row_normalized
        			ids.push_back(id)
        			print("col: %4d, row: %4d, id: %4d" % [col, row, id])
        	
        	print("**************** Reversed **************")
        	var mask_high = 2 ^ BITS_PER_DIMENSION - 1
        	print("mask high: %d" % mask_high)
        	for id in ids:
        		var row_normalized = id & mask_high
        		var row = row_normalized - MAX_EXTENT
        		var col_normalized = id >> BITS_PER_DIMENSION
        		var col = col_normalized - MAX_EXTENT
        		print("col: %4d, row: %4d, id: %4d" % [col, row, id])
        

        Result:

        --- Debugging process started ---
        Godot Engine v3.3.2.stable.official - https://godotengine.org
        OpenGL ES 2.0 Renderer: GeForce 940MX/PCIe/SSE2
        OpenGL ES Batching: ON
         
        Bits per dimension: 2, Max extent: 2
        col:   -2, row:   -2, id:    0
        col:   -2, row:   -1, id:    1
        col:   -2, row:    0, id:    2
        col:   -2, row:    1, id:    3
        col:   -1, row:   -2, id:    4
        col:   -1, row:   -1, id:    5
        col:   -1, row:    0, id:    6
        col:   -1, row:    1, id:    7
        col:    0, row:   -2, id:    8
        col:    0, row:   -1, id:    9
        col:    0, row:    0, id:   10
        col:    0, row:    1, id:   11
        col:    1, row:   -2, id:   12
        col:    1, row:   -1, id:   13
        col:    1, row:    0, id:   14
        col:    1, row:    1, id:   15
        **************** Reversed **************
        mask high: 3
        col:   -2, row:   -2, id:    0
        col:   -2, row:   -1, id:    1
        col:   -2, row:    0, id:    2
        col:   -2, row:    1, id:    3
        col:   -1, row:   -2, id:    4
        col:   -1, row:   -1, id:    5
        col:   -1, row:    0, id:    6
        col:   -1, row:    1, id:    7
        col:    0, row:   -2, id:    8
        col:    0, row:   -1, id:    9
        col:    0, row:    0, id:   10
        col:    0, row:    1, id:   11
        col:    1, row:   -2, id:   12
        col:    1, row:   -1, id:   13
        col:    1, row:    0, id:   14
        col:    1, row:    1, id:   15
        

        ???

        Next time please post tits.

        alt text

        ? 1 Reply Last reply Reply Quote 5
        • ?
          A Former User @Gators1
          last edited by A Former User

          @gators1 said in The OFFICIAL programming thread:

          ???

          I’m making a game with a map where everything is a at a position that is a grid reference away from the center. So player X might be at grid reference (-2, 1), and there might be a big impassable rock at (1, 0).

          However, the pathfinding API (the library that tells me how the player moves from one location to another,) requires that each point on the map be expressed/id’d as a single number greater than zero.

          So, how do you take something like (-2,1) or (1,0) and create a single, unsigned, numbers from them like 3 or 14 and then later take 3 or 14 and turn it back into (-2,1) and (1,0)? That’s what my code does.

          Gators1G 1 Reply Last reply Reply Quote 2
          • ?
            A Former User
            last edited by

            Oh, and:

            a4341b6d-019a-4482-b611-244f75e7e369-image.png

            B 1 Reply Last reply Reply Quote 8
            • Gators1G
              Gators1 @A Former User
              last edited by

              @hog said in The OFFICIAL programming thread:

              @gators1 said in The OFFICIAL programming thread:

              ???

              I’m making a game with a map where everything is a at a position that is a grid reference away from the center. So player X might be at grid reference (-2, 1), and there might be a big impassable rock at (1, 0).

              However, the pathfinding API (the library that tells me how the player moves from one location to another,) requires that each point on the map be expressed as a single number greater than zero.

              So, how do you take something like (-2,1) or (1,0) and create a single, unsigned, numbers from them like 3 or 14 and then later take 3 or 14 and turn it back into (-2,1) and (1,0)? That’s what my code does.

              Wait, did Jerry hire you to work on his game? You know it’s a scam, right?

              alt text

              1 Reply Last reply Reply Quote 3
              • rote7R
                rote7 @A Former User
                last edited by rote7

                @hog said in The OFFICIAL programming thread:

                const BITS_PER_DIMENSION = 2
                const MAX_EXTENT = 1 << (BITS_PER_DIMENSION - 1)
                
                func _ready():
                	print("Bits per dimension: %d, Max extent: %d" % [BITS_PER_DIMENSION, MAX_EXTENT])
                	var ids = []
                	for col in range(-MAX_EXTENT, MAX_EXTENT):
                		var col_normalized = col + MAX_EXTENT
                		var col_shifted = col_normalized << BITS_PER_DIMENSION
                		for row in range(-MAX_EXTENT, MAX_EXTENT):
                			var row_normalized = row + MAX_EXTENT
                			var id = col_shifted + row_normalized
                			ids.push_back(id)
                			print("col: %4d, row: %4d, id: %4d" % [col, row, id])
                	
                	print("**************** Reversed **************")
                        var mask_high = (1 << BITS_PER_DIMENSION) - 1
                	print("mask high: %d" % mask_high)
                	for id in ids:
                		var row_normalized = id & mask_high
                		var row = row_normalized - MAX_EXTENT
                		var col_normalized = id >> BITS_PER_DIMENSION
                		var col = col_normalized - MAX_EXTENT
                		print("col: %4d, row: %4d, id: %4d" % [col, row, id])
                

                Result:

                --- Debugging process started ---
                Godot Engine v3.3.2.stable.official - https://godotengine.org
                OpenGL ES 2.0 Renderer: GeForce 940MX/PCIe/SSE2
                OpenGL ES Batching: ON
                 
                Bits per dimension: 2, Max extent: 2
                col:   -2, row:   -2, id:    0
                col:   -2, row:   -1, id:    1
                col:   -2, row:    0, id:    2
                col:   -2, row:    1, id:    3
                col:   -1, row:   -2, id:    4
                col:   -1, row:   -1, id:    5
                col:   -1, row:    0, id:    6
                col:   -1, row:    1, id:    7
                col:    0, row:   -2, id:    8
                col:    0, row:   -1, id:    9
                col:    0, row:    0, id:   10
                col:    0, row:    1, id:   11
                col:    1, row:   -2, id:   12
                col:    1, row:   -1, id:   13
                col:    1, row:    0, id:   14
                col:    1, row:    1, id:   15
                **************** Reversed **************
                mask high: 3
                col:   -2, row:   -2, id:    0
                col:   -2, row:   -1, id:    1
                col:   -2, row:    0, id:    2
                col:   -2, row:    1, id:    3
                col:   -1, row:   -2, id:    4
                col:   -1, row:   -1, id:    5
                col:   -1, row:    0, id:    6
                col:   -1, row:    1, id:    7
                col:    0, row:   -2, id:    8
                col:    0, row:   -1, id:    9
                col:    0, row:    0, id:   10
                col:    0, row:    1, id:   11
                col:    1, row:   -2, id:   12
                col:    1, row:   -1, id:   13
                col:    1, row:    0, id:   14
                col:    1, row:    1, id:   15
                

                My bit shifting days are but a remote memory so you may have to help me understand how

                var id = col_shifted + row_normalized

                does not overwrite the previously stored pattern in col_shifted.

                fffg

                ? 1 Reply Last reply Reply Quote 0
                • B
                  Blanks @A Former User
                  last edited by

                  @hog said in The OFFICIAL programming thread:

                  Oh, and:

                  a4341b6d-019a-4482-b611-244f75e7e369-image.png

                  Maybe you should program this forum. That’s award winning content right there.

                  1 Reply Last reply Reply Quote 1
                  • ?
                    A Former User @rote7
                    last edited by A Former User

                    @rote7 said in The OFFICIAL programming thread:

                    @hog said in The OFFICIAL programming thread:

                    const BITS_PER_DIMENSION = 2
                    const MAX_EXTENT = 1 << (BITS_PER_DIMENSION - 1)
                    
                    func _ready():
                    	print("Bits per dimension: %d, Max extent: %d" % [BITS_PER_DIMENSION, MAX_EXTENT])
                    	var ids = []
                    	for col in range(-MAX_EXTENT, MAX_EXTENT):
                    		var col_normalized = col + MAX_EXTENT
                    		var col_shifted = col_normalized << BITS_PER_DIMENSION
                    		for row in range(-MAX_EXTENT, MAX_EXTENT):
                    			var row_normalized = row + MAX_EXTENT
                    			var id = col_shifted + row_normalized
                    			ids.push_back(id)
                    			print("col: %4d, row: %4d, id: %4d" % [col, row, id])
                    	
                    	print("**************** Reversed **************")
                            var mask_high = (1 << BITS_PER_DIMENSION) - 1
                    	print("mask high: %d" % mask_high)
                    	for id in ids:
                    		var row_normalized = id & mask_high
                    		var row = row_normalized - MAX_EXTENT
                    		var col_normalized = id >> BITS_PER_DIMENSION
                    		var col = col_normalized - MAX_EXTENT
                    		print("col: %4d, row: %4d, id: %4d" % [col, row, id])
                    

                    Result:

                    --- Debugging process started ---
                    Godot Engine v3.3.2.stable.official - https://godotengine.org
                    OpenGL ES 2.0 Renderer: GeForce 940MX/PCIe/SSE2
                    OpenGL ES Batching: ON
                     
                    Bits per dimension: 2, Max extent: 2
                    col:   -2, row:   -2, id:    0
                    col:   -2, row:   -1, id:    1
                    col:   -2, row:    0, id:    2
                    col:   -2, row:    1, id:    3
                    col:   -1, row:   -2, id:    4
                    col:   -1, row:   -1, id:    5
                    col:   -1, row:    0, id:    6
                    col:   -1, row:    1, id:    7
                    col:    0, row:   -2, id:    8
                    col:    0, row:   -1, id:    9
                    col:    0, row:    0, id:   10
                    col:    0, row:    1, id:   11
                    col:    1, row:   -2, id:   12
                    col:    1, row:   -1, id:   13
                    col:    1, row:    0, id:   14
                    col:    1, row:    1, id:   15
                    **************** Reversed **************
                    mask high: 3
                    col:   -2, row:   -2, id:    0
                    col:   -2, row:   -1, id:    1
                    col:   -2, row:    0, id:    2
                    col:   -2, row:    1, id:    3
                    col:   -1, row:   -2, id:    4
                    col:   -1, row:   -1, id:    5
                    col:   -1, row:    0, id:    6
                    col:   -1, row:    1, id:    7
                    col:    0, row:   -2, id:    8
                    col:    0, row:   -1, id:    9
                    col:    0, row:    0, id:   10
                    col:    0, row:    1, id:   11
                    col:    1, row:   -2, id:   12
                    col:    1, row:   -1, id:   13
                    col:    1, row:    0, id:   14
                    col:    1, row:    1, id:   15
                    

                    My bit shifting days are but a remote memory so you may have to help me understand how

                    var id = col_shifted + row_normalized

                    does not overwrite the previously stored pattern in col_shifted.

                    Sure, it’s a combination of three things:

                    • they’ve both been converted to unsigned ints (normalized) so the highest bit isn’t set in either number
                    • and the lowest bits of col_shifted are 0 because of the shift
                    • row_normalized only has the lowest bits set because the size of the number is constrained to +/- MAX_EXTENTS

                    So, even with maximum values for row and column, the addition is like:

                    00001100 col_shifted +
                    00000011 row_normalized =
                    --------
                    00001111
                    

                    Edit: by the way, the code i posted earlier was just a proof of concept. My actual use case (which I’ve since coded) has three dimensions. It is using 22 bits (9 + 9 + 4) for a map of 512x512x16 possible locations.

                    1 Reply Last reply Reply Quote 1
                    • ?
                      A Former User
                      last edited by

                      This post is deleted!
                      1 Reply Last reply Reply Quote 0
                      • PakoonP
                        Pakoon
                        last edited by

                        E5dT-87WUAQHOqV.jpg

                        ♙♙♙ Michael Waltz added you to the group.

                        tiggerT 1 Reply Last reply Reply Quote 2
                        • tiggerT
                          tigger @Pakoon
                          last edited by

                          @pakoon said in The OFFICIAL programming thread:

                          E5dT-87WUAQHOqV.jpg

                          Jajajaja!

                          1 Reply Last reply Reply Quote 0
                          • rote7R
                            rote7
                            last edited by

                            Time for my favorite frownie: m(

                            fffg

                            1 Reply Last reply Reply Quote 0
                            • rote7R
                              rote7
                              last edited by

                              The Case Against SQL - Slashdot

                              The Case Against SQL - Slashdot

                              Long-time Slashdot reader RoccamOccam shares "an interesting take on SQL and its issues from Jamie Brandon (who describes himself as an independent researcher who's built database engines, query planners, compilers, developer tools and interfaces). It's title? "Against SQL." The relational model...

                              Interesting article, I didn’t know that

                              “The SQL spec (part 2 = 1732) pages is more than twice the length of the Javascript 2021 spec (879 pages), almost matches the C++ 2020 spec (1853) pages and contains 411 occurrences of ‘implementation-defined’, occurrences which include type inference and error propagation.”

                              Sweet jesus, double the length of the JS specs…

                              One SQL problem that I couldn’t solve so far is finding a good way to use the output of a single column query (multiple rows, result count must be flexible) as headers and/or fields for a subsequent query.

                              fffg

                              KilemallK Gators1G 2 Replies Last reply Reply Quote 0
                              • KilemallK
                                Kilemall Careful, railroad agent @rote7
                                last edited by

                                @rote7 said in The OFFICIAL programming thread:

                                The Case Against SQL - Slashdot

                                The Case Against SQL - Slashdot

                                Long-time Slashdot reader RoccamOccam shares "an interesting take on SQL and its issues from Jamie Brandon (who describes himself as an independent researcher who's built database engines, query planners, compilers, developer tools and interfaces). It's title? "Against SQL." The relational model...

                                Interesting article, I didn’t know that

                                “The SQL spec (part 2 = 1732) pages is more than twice the length of the Javascript 2021 spec (879 pages), almost matches the C++ 2020 spec (1853) pages and contains 411 occurrences of ‘implementation-defined’, occurrences which include type inference and error propagation.”

                                Sweet jesus, double the length of the JS specs…

                                One SQL problem that I couldn’t solve so far is finding a good way to use the output of a single column query (multiple rows, result count must be flexible) as headers and/or fields for a subsequent query.

                                Dynamic DB-driven filter query? Sounds like a specific SQL client function to me.

                                https://i.imgur.com/hX2CMMZ.jpg

                                Never go full Lithu-
                                Twain

                                No editing is gonna save you now-
                                Wingmann

                                http://s3.amazonaws.com/rrpa_photos/72217/DSC_2528.JPG

                                http://s3.amazonaws.com/rrpa_photos/20416/PTOB 101_resize.jpg

                                1 Reply Last reply Reply Quote 0
                                • rote7R
                                  rote7
                                  last edited by rote7

                                  @Kilemall Yes, that was my first idea too. My goal is to replace a whole lot of client logic with a set of queries/subqueries and/or joins. I am looking for a pivot mechanism to transform a set of column(s) into rows.

                                  E.g. turn

                                  col_1 | col_2
                                  param_1 | value_1
                                  param_2 | value_2
                                  param_foo | value_fo

                                  into a dynamic DB operation that delivers a result like the client generated query “SELECT * FROM table WHERE param_1 = value_1 AND param_2 = value_2 AND param_foo = value_foo” without all the string concatenation, input scrubbing etc. pp. going on in the client.

                                  fffg

                                  1 Reply Last reply Reply Quote 0
                                  • Gators1G
                                    Gators1 @rote7
                                    last edited by

                                    @rote7 said in The OFFICIAL programming thread:

                                    The Case Against SQL - Slashdot

                                    The Case Against SQL - Slashdot

                                    Long-time Slashdot reader RoccamOccam shares "an interesting take on SQL and its issues from Jamie Brandon (who describes himself as an independent researcher who's built database engines, query planners, compilers, developer tools and interfaces). It's title? "Against SQL." The relational model...

                                    Interesting article, I didn’t know that

                                    “The SQL spec (part 2 = 1732) pages is more than twice the length of the Javascript 2021 spec (879 pages), almost matches the C++ 2020 spec (1853) pages and contains 411 occurrences of ‘implementation-defined’, occurrences which include type inference and error propagation.”

                                    Sweet jesus, double the length of the JS specs…

                                    One SQL problem that I couldn’t solve so far is finding a good way to use the output of a single column query (multiple rows, result count must be flexible) as headers and/or fields for a subsequent query.

                                    I think you need to do it as a stored procedure if I remember right and using the PIVOT function. Basically instead of defining the static columns as in the base function, you dynamically insert them in the stored procedure. If you are doing this as part of another code project, you could do the same by just assembling the text of the SQL script to sent dynamically (e.g. “SELECT “& [column name varIable]&” FROM TABLE_NAME;”

                                    alt text

                                    GustafG 1 Reply Last reply Reply Quote 0
                                    • GustafG
                                      Gustaf @Gators1
                                      last edited by

                                      This thread:

                                      tenor (18).gif

                                      "Let's give it a week! Still a disaster? Let's give it another week…" -Tazz

                                      KilemallK 1 Reply Last reply Reply Quote 2
                                      • rote7R
                                        rote7
                                        last edited by rote7

                                        @Gators1

                                        Stored procedures are one possible solution. But, and this is a big but, they are DB dependent and I am looking for a way to achieve this with as little as possible DB specific SQL dialect.

                                        As for the client generated SQL, that’s the current solution as a part of a homegrown ORM. I am just curious if it can be done purely in the DB as a way to replace a bunch of DB specific logic during the SQL generation in that ORM.

                                        @Gustaf

                                        linux_babe-wet-shirt.jpg

                                        /* insert snappy comment here */

                                        fffg

                                        Gators1G KilemallK GustafG 3 Replies Last reply Reply Quote 0
                                        • ?
                                          A Former User
                                          last edited by

                                          I’d snap one off in her

                                          1 Reply Last reply Reply Quote -1
                                          • Gators1G
                                            Gators1 @rote7
                                            last edited by

                                            @rote7 said in The OFFICIAL programming thread:

                                            @Gators1

                                            Stored procedures are one possible solution. But, and this is a big but, they are DB dependent and I am looking for a way to achieve this with as little as possible DB specific SQL dialect.

                                            As for the client generated SQL, that’s the current solution as a part of a homegrown ORM. I am just curious if it can be done purely in the DB as a way to replace a bunch of DB specific logic during the SQL generation in that ORM.

                                            @Gustaf

                                            linux_babe-wet-shirt.jpg

                                            /* insert snappy comment here */

                                            @rote7
                                            Yeah, I couldn’t find a way other than a stored procedure in Oracle. The function requires you to define the columns in the SQL so it’s static. Nothing in many searches I did changed that, though the procedure thing was suggested. Seems like a stupid design since pivoting often requires dynamic results.

                                            @Gustaf
                                            8f1cc3e9-7371-4564-9386-b9c36376d1ea-image.png

                                            alt text

                                            TazzT 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post