A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: How to change a single bit in as3

  1. #1
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404

    How to change a single bit in as3

    Hi, I will be showing you how to change a bit state to 0 or 1 which is basically a flip of a single transistor state on/off each byte has 8 bits in this tutorial the start bit (0) and the stop bit (9) are useless because we can not store shorts as 0111 they will turn into a 111 we will be taking advantage of 6 bits per byte(8bits) the reason this is useful is because new games like slither.io use octets and need to change the huge map very quickly and at the same time have it ready for new clients so they have a 0 or a 1 in bit form (not byte form), and from there you just have the client keep up with the server for what to change next.

    In this script you have 3000x3000 square grid ready to be accessed, which means you can change 9 million bits in a 2d square map and only send it over through a server and back as only 142bytes (deflated) and it deflates very quickly, I ran the function calls that setBits and getBits through a for loop and you don't notice any performance issues on a fairly old dual-core processor

    the last two lines of code are all you need to set the bit on the square grid and to get its bit on the position you req


    PHP Code:
    var actionStorage={ba:ByteArray,hexLib:[null,"111","222","211","112","121","212","221","122"],map_size:250,temp_reg:"",temp_bit:"",ba_old:"",bitConvertor:[0,1,2,3,4,5,6,7,8,9,10,11],found_bin:"",bit_0:"",bit_1:"",bit_2:"",bit_3:"",bit_4:"",bit_5:"",bit_6:"",bit_7:"",bit_8:"",bit_9:"",bit_10:"",bit_11:"",error_msg:[trace,"error: max range for x and y is 0-3000","Error: flip states can only be 0 or 1, you used something else"]};
    actionStorage.ba=new ByteArray();

    for(var 
    i=0;i<int(int(actionStorage.map_size)+1)*int(int(actionStorage.map_size)+1);i++){
    actionStorage.ba.writeShort(2222);
    }
    actionStorage.ba.deflate();


    function 
    getBit(temp_x,temp_y){
    if(
    temp_x<3001&&temp_x>-1&&temp_y<3001&&temp_y>-1){
    actionStorage.ba.inflate();
    if(
    temp_y>0){
    actionStorage.ba.position =int(temp_x) +  ((int(temp_y)*actionStorage.map_size))
    }else{
    actionStorage.ba.position =int(temp_x) +   ((int(temp_y)*actionStorage.map_size))    
    }
    actionStorage.ba_old=actionStorage.ba.position
    actionStorage
    .temp_reg= (actionStorage.ba.position/12)+".0";
    actionStorage.temp_reg=actionStorage.temp_reg.split(".")[0]
    actionStorage.ba.position=actionStorage.temp_reg*2;
    actionStorage.found_bin=actionStorage.ba.readShort().toString();
    actionStorage.ba.deflate();
    return(
    actionStorage["bit_"+actionStorage.bitConvertor[actionStorage.ba_old%12]]-1);
    }else{
    actionStorage.error_msg[0](actionStorage.error_msg[1])
    }
    }


    function 
    setBit(temp_x,temp_y,flip_state){;
    if(
    flip_state==0||flip_state==1){
    actionStorage.ba.inflate()
    flip_state++;
    if(
    temp_y>0){
    actionStorage.ba.position =int(temp_x) +  ((int(temp_y)*actionStorage.map_size))
    }else{
    actionStorage.ba.position =int(temp_x) +   ((int(temp_y)*actionStorage.map_size))    
    }
    actionStorage.ba_old=actionStorage.ba.position
    actionStorage
    .temp_reg= (actionStorage.ba.position/12)+".0";
    actionStorage.temp_reg=actionStorage.temp_reg.split(".")[0]
    actionStorage.ba.position=actionStorage.temp_reg*2;
    actionStorage.found_bin=actionStorage.ba.readShort().toString();
    actionStorage.ba.position-=2;
    actionStorage.bit_0=actionStorage.hexLib[actionStorage.found_bin.charAt(0)].charAt(0);
    actionStorage.bit_1=actionStorage.hexLib[actionStorage.found_bin.charAt(0)].charAt(1);
    actionStorage.bit_2=actionStorage.hexLib[actionStorage.found_bin.charAt(0)].charAt(2);
    actionStorage.bit_3=actionStorage.hexLib[actionStorage.found_bin.charAt(1)].charAt(0);
    actionStorage.bit_4=actionStorage.hexLib[actionStorage.found_bin.charAt(1)].charAt(1);
    actionStorage.bit_5=actionStorage.hexLib[actionStorage.found_bin.charAt(1)].charAt(2);
    actionStorage.bit_6=actionStorage.hexLib[actionStorage.found_bin.charAt(2)].charAt(0);
    actionStorage.bit_7=actionStorage.hexLib[actionStorage.found_bin.charAt(2)].charAt(1);
    actionStorage.bit_8=actionStorage.hexLib[actionStorage.found_bin.charAt(2)].charAt(2);
    actionStorage.bit_9=actionStorage.hexLib[actionStorage.found_bin.charAt(3)].charAt(0);
    actionStorage.bit_10=actionStorage.hexLib[actionStorage.found_bin.charAt(3)].charAt(1);
    actionStorage.bit_11=actionStorage.hexLib[actionStorage.found_bin.charAt(3)].charAt(2);
    actionStorage["bit_"+actionStorage.bitConvertor[actionStorage.ba_old%12]]=flip_state;
    actionStorage.ba.writeShort(int(actionStorage.hexLib.indexOf(actionStorage.bit_0+actionStorage.bit_1+actionStorage.bit_2).toString()+actionStorage.hexLib.indexOf(actionStorage.bit_3+actionStorage.bit_4+actionStorage.bit_5).toString()+actionStorage.hexLib.indexOf(actionStorage.bit_6+actionStorage.bit_7+actionStorage.bit_8).toString()+actionStorage.hexLib.indexOf(actionStorage.bit_9+actionStorage.bit_10+actionStorage.bit_11).toString()));
    actionStorage.ba.deflate();
    }else{
    actionStorage.error_msg[0](actionStorage.error_msg[2])
    }
    }

    setBit(3000,3000,0);
    trace(getBit(3000,3000)); 
    Last edited by AS3.0; 01-23-2017 at 06:48 PM.

  2. #2
    Client Software Programmer AS3.0's Avatar
    Join Date
    Apr 2011
    Posts
    1,404
    This is the final version just to make things shorter:


    PHP Code:
    var actionStorage={ba:ByteArray,pattern:[0,0,0,1,1,1,2,2,2,3,3,3],hexLib:[null,"111","222","211","112","121","212","221","122"],map_size:250,temp_reg:"",temp_bit:"",ba_old:"",bitConvertor:[0,1,2,3,4,5,6,7,8,9,10,11],found_bin:"",bit_0:"",bit_1:"",bit_2:"",bit_3:"",bit_4:"",bit_5:"",bit_6:"",bit_7:"",bit_8:"",bit_9:"",bit_10:"",bit_11:"",error_msg:[trace,"error: max range for x and y is 0-3000","Error: flip states can only be 0 or 1, you used something else"]};
    actionStorage.ba=new ByteArray();

    for(var 
    i=0;i<int(int(actionStorage.map_size)+1)*int(int(actionStorage.map_size)+1);i++){
    actionStorage.ba.writeShort(2222);
    }
    actionStorage.ba.deflate();


    function 
    getBit(temp_x,temp_y){
    if(
    temp_x<3001&&temp_x>-1&&temp_y<3001&&temp_y>-1){
    actionStorage.ba.inflate();
    if(
    temp_y>0){
    actionStorage.ba.position =int(temp_x) +  ((int(temp_y)*actionStorage.map_size))
    }else{
    actionStorage.ba.position =int(temp_x) +   ((int(temp_y)*actionStorage.map_size))    
    }
    actionStorage.ba_old=actionStorage.ba.position
    actionStorage
    .temp_reg= (actionStorage.ba.position/12)+".0";
    actionStorage.temp_reg=actionStorage.temp_reg.split(".")[0]
    actionStorage.ba.position=actionStorage.temp_reg*2;
    actionStorage.found_bin=actionStorage.ba.readShort().toString();
    actionStorage.ba.deflate();
    for(
    i=0;i<12;i++){
    actionStorage["bit_"+i]=actionStorage.hexLib[actionStorage.found_bin.charAt(actionStorage.pattern[i])].charAt(i%3);
    }
    return(
    actionStorage["bit_"+actionStorage.bitConvertor[actionStorage.ba_old%12]]-1);
    }else{
    actionStorage.error_msg[0](actionStorage.error_msg[1])
    }
    }


    function 
    setBit(temp_x,temp_y,flip_state){;
    if(
    flip_state==0||flip_state==1){
    actionStorage.ba.inflate()
    flip_state++;
    if(
    temp_y>0){
    actionStorage.ba.position =int(temp_x) +  ((int(temp_y)*actionStorage.map_size))
    }else{
    actionStorage.ba.position =int(temp_x) +   ((int(temp_y)*actionStorage.map_size))    
    }
    actionStorage.ba_old=actionStorage.ba.position
    actionStorage
    .temp_reg= (actionStorage.ba.position/12)+".0";
    actionStorage.temp_reg=actionStorage.temp_reg.split(".")[0]
    actionStorage.ba.position=actionStorage.temp_reg*2;
    actionStorage.found_bin=actionStorage.ba.readShort().toString();
    actionStorage.ba.position-=2;
    for(
    i=0;i<12;i++){
    actionStorage["bit_"+i]=actionStorage.hexLib[actionStorage.found_bin.charAt(actionStorage.pattern[i])].charAt(i%3);
    }
    actionStorage["bit_"+actionStorage.bitConvertor[actionStorage.ba_old%12]]=flip_state;
    actionStorage.ba.writeShort(int(actionStorage.hexLib.indexOf(actionStorage.bit_0+actionStorage.bit_1+actionStorage.bit_2).toString()+actionStorage.hexLib.indexOf(actionStorage.bit_3+actionStorage.bit_4+actionStorage.bit_5).toString()+actionStorage.hexLib.indexOf(actionStorage.bit_6+actionStorage.bit_7+actionStorage.bit_8).toString()+actionStorage.hexLib.indexOf(actionStorage.bit_9+actionStorage.bit_10+actionStorage.bit_11).toString()));
    actionStorage.ba.deflate();
    }else{
    actionStorage.error_msg[0](actionStorage.error_msg[2])
    }
    }

    setBit(0,0,0);  
    trace(getBit(0,0)); 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center