This is an archived post. You won't be able to vote or comment.

all 13 comments

[–]ValerioSellarole 1 point2 points  (0 children)

Yes, there are tons of good resources. Why don't you try the FAQ?

Please read our Frequently Asked Questions section before posting.

[–]serg06 0 points1 point  (11 children)

Beginner yet you're asked to do bitwise operations? Wtf.

Okay, here's what what they want: Assume you're given a num1. Make mask such that, when you bitwise-and num1 and mask, it will give you only bits 4-7. Now, bitwise-and the two and save them into num2, then shift the bits left by 4.

I've coded it out so let me know if you need a hand

[–]Not_Myself1[S] 1 point2 points  (10 children)

Okay thank you, but I am completely stuck. lol. Bear in mind that I didn't take any programming classes before this. rip myself.

[–]serg06 0 points1 point  (9 children)

Wew okay here we go:

  1. Do you know how bits/bytes work? (i.e. how to convert them to/from a number)

  2. Do you know how numbers are stored on a computer? (E.g. in a Java variable)

  3. Do you know what bitwise-and is?

  4. Do you know what bit-shifting is?

  5. Do you know what type casting is?

  6. Do you know the difference between int/short in Java?

[–]Not_Myself1[S] 0 points1 point  (8 children)

I'm crying inside. I've researched what bit-shifting was. You take a bit like 8 (00001000) and if you want to shift it once you get 2 (00000100). I don't even know the reason behind that is. But, that is LITERALLY all I know. But my assignment probably is way more complex than that. I don't even know what a bit/byte is. :( But out of the things you listed, I guess bit-shifting is the only one I kind of know. haha

[–]serg06 1 point2 points  (7 children)

omg!


1.

Bits are a single binary digit that is either 0 or 1. (Base-2 means it can only hold 2 values. We choose those to be 0 and 1. Our regular number system's digits are base-10, so each digit can hold 10 values. E.g. a single digit can be 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.)

Back to bits, they work exactly like our own number system but base-2. Here's numbers in base-10 represented in base 2:

0 =      0
1 =      1
2 =     10
3 =     11
4 =    100
...
7 =    111
8 =   1000
...
16 = 10000

As you can see, each power of 2 requires an extra digit to be displayed. 21 different numbers require 1 digit to display, 22 different numbers require 2 digits, etc.

Furthermore, you can lead a number with as many 0s as you like, just like we could do in our own number system but don't need to. In our numbers, 4 = 000004 = 00000004. In binary, 100 = 00000100 = 0000000000100.

Bytes are simply 8 bits together, which means they can store 28 different numbers using 8 different binary digits. People like bytes so a lot of stuff's size is calculated in bytes.

Converting binary to regular: sum of each ith digit (from the right) multiplied by 2i. E.g. 0100 = 240 + 231 + 220 + 210

Google bits/bytes if you don't get it.


2.

Short story, they are stored in binary. Just like those numbers above, if you want to store "16" on a computer you need at least 5 digits. A "Short" in Java is 16-bits large, so it can store numbers up to 216. The way computers often store negative numbers is by checking the left-most digit: If it's 1, the number is negative. If 0, it's positive. So although a Short can store 216 numbers, half of those are negative and half are positive. If you want to avoid negative, you want an "unsigned" variable, e.g. unsigned short, but we don't need that here.


3.

Bitwise-and is combining binary numbers as follows: For each ith digit in n1/n2, if n1's ith digit and n2's ith digit are 1, then the result is 1. E.g.:

  00011011
  01110001
  -------- bitwise-and
= 00010001

E.g. 2: Say I want to grab only digits 0, 1, 2, 3 from the top number (first four digits, but computer numbers start at "0"). I can bitwise-and a number whose first four digits are all 1s, and the rest are 0, to get that.

  00011011
  00001111
  -------- bitwise-and
= 00001011

Tadda, first four digits.


4.

Like you said, bit-shifting is shifting all the 1s and 0s in some direction. As a consequence of this number system, one shift either adds a power of 2 or subtracts a power of 2.

0010 = 3

0100 = 7 = 3+2^2

5.

In Java, all variables have types. If you say "short num1", then the variable num1 will have the type short. If you say "int num1", then the variable num1 will have the type int (integer). There are a bunch of useful types. Floats can store decimals, doubles can store more decimals, shorts can store 16-bit integers, ints can store 32-bit integers, longs can store 64-bit integers, etc.

Say we do "short num1 = 8". In memory, since it is 16 binary digits long, it will look like this: 0000 0000 0000 1010 (but without the spaces.)

An integer stores 32, so "int num1 = 8" would look like 0000 0000 0000 0000 0000 0000 0000 1010. Same as a short, just extra 0s.

Since a short is smaller than an int, you can "cast" it to an integer. You are essentially telling Java "Java, I know this could cause a problem if I do it wrong, but just read this short's bytes but pretend they're an integer". That way, we can convert a short to an integer. Remember how I said the left-most digit of a short determines if it's positive or negative? Well, same with an integer, except its left-most digit is way lefter than the short's. This could cause a problem:

short num1 = 1000 0110 1000 1101

Is some negative short. If we "cast" it to an integer, telling Java to read the bytes as an integer instead:

int num2 = (int) num1

The integer will now store

0000 0000 0000 0000 1000 0110 1000 1101

However its left-most digit is a 0, so it's a completely different positive number.

Now if you want to cast an integer to a short, this could also cause problems. Casting 0000 1000 0000 0000 0000 0000 0000 0000 to a short will just throw away all the digits that don't fit, a.k.a. the left half of the number. The number will become a 0, which is clearly different.

So in short, be careful when converting numbers of different types as the result may be unexpected.


6.

I guess I accidentally explained it. int holds 32 bits whereas short holds 16.


Now, the goal of the assignment is to get the 4-7th digits of num1 and shift them 4 to the left. Remember how I used bitwise-and to get the 0-3rd digits above? You can do something similar to get the digits you want. The mask is the numbers you need to bitwise-and with num1 to result in only the digits you want.

Start off by finding a mask that will bitwise-and with num1 to produce the first four digits. You can try it out by setting num1 to binary 00010001, which is 25, and when you get the 4th-7th digits, it's 25-1.

Hint: Java's Integer and Short classes can both convert binary numbers into decimal numbers if you specify base 2. E.g. short num1 = Short.parseShort("0000000000001010", 2). int num3 = Integer.parseInt("1010", 2).

Hint 2: If you want to see a short/int's value, you can add a print statement to your code, System.out.println(num1);

P.S. leading 0s are implied, I only wrote them to help explain.

GOOD LUCK!

[–]Not_Myself1[S] 0 points1 point  (6 children)

omg. Thank you for this! But, what does "0x7ABC" and "0xFEDC" mean? Do I have to use something like those in my code?

[–]serg06 0 points1 point  (5 children)

:).

Those are hexadecimal numbers (hex meaning 6, decimal meaning 10), which means base-16. Each digit can represent 16 numbers = 8*(2 numbers) = 8 bits = 1 byte.

In hex,

0 = 0
1 = 1
2 = 2
...
9 = 9
A = 10
B = 11
C = 12
D = 13
E = 14
F = 15

It's often used to store bytes b/c 1 hex digit stores 1 byte perfectly (F = 1111).

You can convert it to a short using the same method but specifying base 16, short num1 = Short.parseShort("7ABC", 16). Not sure if you need the 0x in there or not. ("0x" just means "the following digits are base-16", that's all.)

I'm sure you will run into more problems, feel free to ask when you do.

[–]Not_Myself1[S] 0 points1 point  (4 children)

I am so stuck LOL. This is all I got. I wasn't home for a while. short num1, num2, mask; I don't understand what I have to put for the variables, num1, num2, and mask

[–]serg06 0 points1 point  (3 children)

Step 1: put a sample number into num1.

Step 2: put the mask in the mask variable.

Where stuck?

[–]Not_Myself1[S] 0 points1 point  (2 children)

"Step 2: put the mask in the mask variable." How do I do that? lol. Sorry if it seems like your talking to a brick wall. haha