{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-04-06T14:01:22.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2026-04-06T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":808,"title":"Hamming Weight - Fast","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Vector of number of ones of the binary representation\r\n\r\n*Scoring:* Time in milliseconds to process a [4096*4096,1] vector\r\n\r\n*Examples:* Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\r\n\r\n*Timing Test vector:* uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n*Minimum vector length/increment:* 65536\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF \r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.\r\n\r\n\r\nHint: Globals are not good for time performance.\r\n\r\nHint: Segmentation appears to provide significant time optimization potential.\r\n\r\n\r\n\r\n","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Vector of number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/p\u003e\u003cp\u003e\u003cb\u003eTiming Test vector:\u003c/b\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003e\u003cb\u003eMinimum vector length/increment:\u003c/b\u003e 65536\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e\u003cp\u003eHint: Globals are not good for time performance.\u003c/p\u003e\u003cp\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',2000);\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=2000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 65536 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 65536 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time))\r\n%%\r\nglobal net_time\r\n% net_time in ms\r\n% Create graph data\r\nnet_time=min(2000,net_time); % Limit graph y-axis\r\n\r\nfeval(@assignin,'caller','score',floor(net_time));\r\n\r\n%fh=fopen('Ham.m','wt');\r\n%fprintf(fh,'%s\\n',repmat('1;',[1,round(net_time/2)]));\r\n%fclose(fh);","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2012-11-22T11:18:57.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-30T05:08:08.000Z","updated_at":"2026-02-09T12:51:11.000Z","published_at":"2012-07-01T05:19:12.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of length N of 32 bit integer values.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of number of ones of the binary representation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Input [7 ; 3], output=[3;2]; [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eTiming Test vector:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eMinimum vector length/increment:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 65536\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHelpful, possibly, global variables.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Globals are not good for time performance.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":810,"title":"Hamming Weight - Size Scoring","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Total number of ones of the binary representation\r\n\r\n*Scoring:* Normal Cody Size, while solving multiple cases without timing out\r\n\r\nExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\r\n\r\nStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Total number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/p\u003e\u003cp\u003eExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/p\u003e\u003cp\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=4000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 4096 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096*4096 values %.0f msec\\n',net_time))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":10,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-07-01T02:47:44.000Z","updated_at":"2012-07-01T05:25:20.000Z","published_at":"2012-07-01T05:25:20.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of length N of 32 bit integer values.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Total number of ones of the binary representation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExamples: Input [7 ; 3], output=[3 ; 2]; [16 32], output [1 ; 1]; [0 4294967295] output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHelpful, possibly, global variables.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"errors":[],"problems":[{"id":808,"title":"Hamming Weight - Fast","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Vector of number of ones of the binary representation\r\n\r\n*Scoring:* Time in milliseconds to process a [4096*4096,1] vector\r\n\r\n*Examples:* Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\r\n\r\n*Timing Test vector:* uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n*Minimum vector length/increment:* 65536\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF \r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.\r\n\r\n\r\nHint: Globals are not good for time performance.\r\n\r\nHint: Segmentation appears to provide significant time optimization potential.\r\n\r\n\r\n\r\n","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Vector of number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/p\u003e\u003cp\u003e\u003cb\u003eExamples:\u003c/b\u003e Input [7 ; 3], output=[3;2];  [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/p\u003e\u003cp\u003e\u003cb\u003eTiming Test vector:\u003c/b\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003e\u003cb\u003eMinimum vector length/increment:\u003c/b\u003e 65536\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1).\r\nnum_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e\u003cp\u003eHint: Globals are not good for time performance.\u003c/p\u003e\u003cp\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nfeval(@assignin,'caller','score',2000);\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=2000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 65536 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 65536 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[65536*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time))\r\n%%\r\nglobal net_time\r\n% net_time in ms\r\n% Create graph data\r\nnet_time=min(2000,net_time); % Limit graph y-axis\r\n\r\nfeval(@assignin,'caller','score',floor(net_time));\r\n\r\n%fh=fopen('Ham.m','wt');\r\n%fprintf(fh,'%s\\n',repmat('1;',[1,round(net_time/2)]));\r\n%fclose(fh);","published":true,"deleted":false,"likes_count":0,"comments_count":1,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":18,"test_suite_updated_at":"2012-11-22T11:18:57.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2012-06-30T05:08:08.000Z","updated_at":"2026-02-09T12:51:11.000Z","published_at":"2012-07-01T05:19:12.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of length N of 32 bit integer values.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of number of ones of the binary representation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Time in milliseconds to process a [4096*4096,1] vector\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eExamples:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Input [7 ; 3], output=[3;2]; [16 32], output [1;1]; [0 4294967295] output [0;32]\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eTiming Test vector:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e uint32(randi(2^32,[4096*4096,1])-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eMinimum vector length/increment:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e 65536\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHelpful, possibly, global variables.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Globals are not good for time performance.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHint: Segmentation appears to provide significant time optimization potential.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":810,"title":"Hamming Weight - Size Scoring","description":"The Hamming Weight, \u003chttp://en.wikipedia.org/wiki/Hamming_weight wiki Hamming Weight\u003e, in its most simple form is the number of ones in the binary representation of a value.\r\n\r\nThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\r\n\r\n*Input:* Vector of length N of 32 bit integer values.\r\n\r\n*Output:* Total number of ones of the binary representation\r\n\r\n*Scoring:* Normal Cody Size, while solving multiple cases without timing out\r\n\r\nExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\r\n\r\nStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\r\n\r\n\r\nHelpful, possibly, global variables.\r\n\r\nb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\r\n\r\nHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\r\n\r\nThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\r\n\r\nDue to lack of zero indexing num_ones(value+1) is number of ones for value.","description_html":"\u003cp\u003eThe Hamming Weight, \u003ca href=\"http://en.wikipedia.org/wiki/Hamming_weight\"\u003ewiki Hamming Weight\u003c/a\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/p\u003e\u003cp\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/p\u003e\u003cp\u003e\u003cb\u003eInput:\u003c/b\u003e Vector of length N of 32 bit integer values.\u003c/p\u003e\u003cp\u003e\u003cb\u003eOutput:\u003c/b\u003e Total number of ones of the binary representation\u003c/p\u003e\u003cp\u003e\u003cb\u003eScoring:\u003c/b\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/p\u003e\u003cp\u003eExamples: Input [7 ; 3], output=[3 ; 2];  [16 32], output [1 ; 1]; [0 4294967295]  output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/p\u003e\u003cp\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/p\u003e\u003cp\u003eHelpful, possibly, global variables.\u003c/p\u003e\u003cp\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/p\u003e\u003cp\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/p\u003e\u003cp\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/p\u003e\u003cp\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/p\u003e","function_template":"function y = Ham(x)\r\n% Input uint32\r\nglobal num_ones b1 b2 b3 b4 b5\r\n  y = x;\r\nend","test_suite":"%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\nnet_time=4000; % default in case of time out (not needed)\r\n\r\nb1=uint32(1431655765); \r\nb2=uint32(858993459);\r\nb3=uint32(252645135);\r\nb4=uint32(16711935);\r\nb5=uint32(65535);\r\n \r\nnum_ones=uint32(zeros(65536,1)); \r\nfor i=0:65535  num_ones(i+1)=length( find( bitget( i, 1:32 ) ) ) ; \r\nend % Cody 0.996 sec\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\nfor i=1:4 % Clear timing\r\n  vw=Ham(w);\r\nend\r\n\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\ndt=etime(clock,t0)*250*1000; % avg of 4 runs in us\r\nfprintf('Time to execute 4096 values %.0f usec\\n',dt);\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096 values %.0f usec\\n',dt))\r\n%%\r\nglobal num_ones b1 b2 b3 b4 b5 net_time\r\n\r\nw=uint32(randi(2^32,[4096*1,1])-1); \r\n\r\n vw=Ham(w); % Three cycles of smaller vector\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n\r\nw=uint32(randi(2^32,[4096*4096,1])-1);\r\nwexpect=num_ones(mod(w,65536)+1)+num_ones(floor(double(w)/65536)+1); %1.56\r\n\r\n \r\n  vw=Ham(w); % Big Prep file\r\n  vw=Ham(w); % Big Prep file\r\n \r\nt0=clock;\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\n vw=Ham(w);\r\nnet_time=etime(clock,t0)*250; % avg of 4 runs\r\nfprintf('Time to execute 4096*4096 values %.0f msec\\n',net_time);\r\n\r\nassert(isequal(wexpect,vw),sprintf('\\nTime to execute 4096*4096 values %.0f msec\\n',net_time))\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":0,"created_by":3097,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":10,"test_suite_updated_at":null,"rescore_all_solutions":false,"group_id":1,"created_at":"2012-07-01T02:47:44.000Z","updated_at":"2012-07-01T05:25:20.000Z","published_at":"2012-07-01T05:25:20.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe Hamming Weight,\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"http://en.wikipedia.org/wiki/Hamming_weight\\\"\u003e\u003cw:r\u003e\u003cw:t\u003ewiki Hamming Weight\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e, in its most simple form is the number of ones in the binary representation of a value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe task here is to create a fast Hamming Weight function/method such that processing many 4K x 4K images of 32 bit integer can be evaluated rapidly. Saw this question posed on Stack Overflow.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eInput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Vector of length N of 32 bit integer values.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eOutput:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Total number of ones of the binary representation\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eScoring:\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e Normal Cody Size, while solving multiple cases without timing out\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eExamples: Input [7 ; 3], output=[3 ; 2]; [16 32], output [1 ; 1]; [0 4294967295] output [ 0 ; 32] FFFFFFFF Hex = 2^32-1\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eStressing Test vector : uint32(randi(2^32,[4096*4096,1])-1)\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHelpful, possibly, global variables.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eb1=uint32(1431655765); b2=uint32(858993459); b3=uint32(252645135) b4=uint32(16711935); b5=uint32(65535);\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHex: b1=55555555 b2=33333333 b3=0F0F0F0F b4=00FF00FF b5=0000FFFF\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe array num_ones is created for values 0-65535 (0:2^16-1). num_ones(1)=0, num_ones(2)=1, num_ones(3)=1,num_ones(4)=2,...num_ones(65536)=15\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eDue to lack of zero indexing num_ones(value+1) is number of ones for value.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"term":"tag:\"floor\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"floor\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"floor\"","","\"","floor","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f234a818520\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f234a818480\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f234a8179e0\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f234a8187a0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f234a818700\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f234a818660\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f234a8185c0\u003e":"tag:\"floor\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f234a8185c0\u003e":"tag:\"floor\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"cody-search","password":"78X075ddcV44","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"floor\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"floor\"","","\"","floor","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f234a818520\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f234a818480\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f234a8179e0\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f234a8187a0\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f234a818700\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f234a818660\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f234a8185c0\u003e":"tag:\"floor\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f234a8185c0\u003e":"tag:\"floor\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":808,"difficulty_rating":"easy"},{"id":810,"difficulty_rating":"medium"}]}}