Bit array of Java

Source: Internet
Author: User

Write a small thing, to repeat the number, use a bit array, although Java has provided a bitset, but preface, and wrote a simple

The principle is not written, a lot of online

1 ImportJava.util.Iterator;2 ImportJava.util.function.BiConsumer;3 4  Public classBitArrayImplementsIterable<boolean>{5     //represents the value of the 1<<n, improving efficiency without each calculation6     Private Final byte[] MASK =New byte[]{1,2,4,8,16,32,64, (byte) 128};7     byte[] bits;8     intMax = 0;9     Ten     /** One * Construct a bit array A      * @paramMax maximum number of digits -      */ -      PublicBitArray (intmax) { the          This. Max =Max; -         intLen = max/8 + 1; -          -BITS =New byte[Len]; +     } -      +     /** A * Set the value of nth bit at      * @paramIndex bit Indexes -      * @paramValue Values -      */ -      Public voidSetintIndexBooleanvalue) { -         inti = INDEX/8; -         intMove = index% 8; in          -Bits[i] = (byte) (Bits[i] |Mask[move]); to     } +      -     /** the * Get the value of nth bit *      * @paramIndex bit Indexes $      * @returnPanax Notoginseng      */ -      Public BooleanGetintindex) { the         inti = INDEX/8; +         intMove = index% 8; A          the         return(Bits[i] & mask[move]) = =Mask[move]; +     } -      $     /** $ * Show All bits -      */ -      Public voidShow () { the          for(inti=0; i<bits.length; i++){ -             byteb =Bits[i];Wuyi              for(intbitindex=0; bitindex<8;bitindex++){ theSystem.out.print (((b>>bitindex) & 1) + ""); -             } Wu System.out.println (); -         } About     } $  -     /** - * provides traversal interface -      */ A      PublicIterator<boolean>iterator () { +         return NewIterator<boolean>(){ the             Private inti = 0; -  $              Public BooleanHasnext () { the                 returnI <=Max; the             } the  the              PublicBoolean Next () { -                 returnGet (i++); in             } the              the         }; About     } the      the     /** the * Traversal, lazy use of the new interface JAVA8 +      * @param Fun -      */ the      Public voidForEach (biconsumer<integer,boolean>Fun ) {Bayi         intTotal = 0; the          for(inti=0; i<bits.length; i++){ the             byteb =Bits[i]; -              for(intbitindex=0; Bitindex<8 && total<=max;bitindex++,total++){ -Fun.accept (Total, ((B>>bitindex) & 1) = = 1); the             } the         } the     } the}

How to use:

 Public Static voidMain (string[] args)throwsexception{BitArray bits=NewBitArray (18); Bits.set (18,true); System.out.println ("Position 3:" + bits.get (3)); System.out.println ("Position:" + bits.get (18)); System.out.println ("--------------------------"); //Traversal mode one    inti = 0;  for(Boolean result:bits) System.out.println (i++ + " : " +result); System.out.println ("--------------------------"); //Traversal mode twobiconsumer<integer,boolean> fun = (index, value){System.out.println (index+ " : " +value);            }; Bits.foreach (fun);}

Output Result:

/* position 3:falseposition 18:true--------------------------0:false1:false2:false3:false4:false5:false6 : False7:false8:false9:false10:false11:false12:false13:false14:false15:false16:false17:false18:tru E--------------------------0:false1:false2:false3:false4:false5:false6:false7:false8:false9:false10: False11:false12:false13:false14:false15:false16:false17:false18:true* *

Bit array of Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.