1

I can't find a useful answer for this in relation to Java here or on the web in general. The problem is seemingly simple - I need a way of counting the number of bits contained in a byte array of arbitrary size. The byte array can hold hex, decimal or binary values.

I'm sure this is one of those questions that will have me kicking myself when I see an answer, but it's proving extremely frustrating. The method should look something like:

public int bitsInByteArray(byte[] b) {
    return - sum of bit count (ie the sum total bits, not the sum of their values) 
    of each byte in b
}

Any direction or advice would be immensely appreciated.

EDIT - Apologies guys, I didn't word this right at all. I'm doing a crypto assignment, and have been asked to "encrypt a single block containing 64 zero bits using AES with the key k" Now, I created byte[] zeroBlockAes = {0x0,0x0...0x0} until it contains 64 zero elements. The problem is, this is a 512 bits surely? So should I just put eight zero values in?

When I do this, my encrypted output is -75 111 69 107 -18 88 51 89 68 -123 -49 18 -26 -109 94 21

And when I decrypt again I get my original eight zeros. Is this normal in AES or am I possibly doing something wrong, I thought block ciphers produced the same size input / output for both encryption and decryption.

10
  • 6
    return b.length *8; Commented Oct 26, 2012 at 20:16
  • 1
    What do you mean, a byte array holds bytes and nothing else. What is a "hex value", even? Commented Oct 26, 2012 at 20:17
  • Do you have any doubt in the equation: - 1 byte = 8 bits? Then ask it directly, rather than framing a random question out of it. Commented Oct 26, 2012 at 20:18
  • 1
    What if the OP means "the number of 1-bits in the array"? That makes more sense. Commented Oct 26, 2012 at 20:20
  • @harold Quote OP: "not the sum of their values" Commented Oct 26, 2012 at 20:20

2 Answers 2

6

Given that 1 byte equals 8 bits, which Java defines in Byte.SIZE, we can write:

 public int bitsInByteArray(final byte[] b) {
     return b.length * Byte.SIZE;
 }
Sign up to request clarification or add additional context in comments.

Comments

1

as others have already said, if it's a byte array then the answer is to multiply the length by 8 since a byte is always 100% guaranteed to be 8 bits.

However, if you have an array of other types (ints, longs, etc) then you're kind of stuck because Java doesn't have a sizeOf method, which would give the the byte size of a given type.

These types are *usually standardized now, but of course they *could still vary from place to place (it's extremely unlikely), so you couldn't guarantee how much memory, precisely, they're occupying.

Is this a homework assignment? If not... you're probably doing something wrong, if you're asking this question about java.

2 Comments

On the other hand, Java has a handful of rigidly specified types so there's no need for sizeof.
int = 4 byte, long = 8 byte, boolean = 1 byte, float = 4 byte, double = 8 byte, short = char = 2 byte.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.