1

I am currently working on a programming assignment in which I have to mask only a certain index of the whole 32-bit number(EX: If I take 8 4-bit numbers into my 32-bit integer, I would have 8 indices with 4 bits in each). I want to be able to print only part of the bits out of the whole 32 bits, which can be done with masking. If the bits were to only be in one place, I would not have a problem, for I would just create a mask that puts the 1s in a set place(EX: 00000000 00000000 00000000 00000001). However, I need to be able to shift the mask throughout only one index to print each bit (EX: I want to loop through the first index with my 0001 mask, shifting the 1 left every time, but I do not want to continue after the third bit of that index). I know that I need a loop to accomplish this; however, I am having a difficult time wrapping my head around how to complete this part of my assignment. Any tips, suggestions, or corrections would be appreciated. Also, I'm sorry if this was difficult to understand, but I could not find a better way to word it. Thanks.

SlackStack
  • 79
  • 1
  • 2
  • 12
  • In what language is this for? (C, C++, JavaScript, ...). In general you can do a shift operation combined with an AND mask (0xabcd>>4 & 0xf would give you 0x0c). –  Sep 06 '17 at 01:11
  • It's in C++..... – SlackStack Sep 06 '17 at 01:12
  • I want to use a mask and bitwise-AND operator along with shifting, but I am just confused as to how I can create a mask that will change bits along with my input. For example: If I needed a mask for an index with four-bits, I would make 1111, but if I needed a mask for an index of two-bits, then I would have to make 11. – SlackStack Sep 06 '17 at 01:15
  • consider the bitwise shift left operator (or just multiply by 2) – Kenny Ostrom Sep 06 '17 at 01:43

1 Answers1

3

first of all about representation. You need binary numbers to represent bits and masks. There are no binaries implemented directly in c/c++ languages at least before c++14. So, before c++14 you had to use hexadecimals or octals to represent your binaries, i.e.

0000 1111 == 0x0F
1111 1010 == 0xFA

since c++14 you can use

0b00001111;

Now, if you shift your binary mask left or right, you will have the following pictures

00001111 (OxF) << 2 ==> 00111100 (0x3C)
00001111 (0xF) >> 2 ==> 00000011 (0x03)

Now, supposedly you have an number in which you are interested in bits 4 to 7 (4 bits)

int bad = 0x0BAD; // == 0000 1011 1010 1101

you can create a mask as

int mask = 0x00F0; // == 0000 0000 1111 00000

and do bitwise and

int result = bad & mask; // ==> 0000 0000 1010 000 (0x00A0)

You will mask 4 bits in the middle of the word, but it will print as 0xA0. probably not what you would expect. To print it as 0xA you would need to shift the result 4 bits right: result >> 4. I prefer doing it in a bit different order, shifting the 'bad' first and then mask:

int result = (bad >> 4) & 0xF;

I hope the above will help you to understand bits.

Serge
  • 11,616
  • 3
  • 18
  • 28
  • Okay, I have a quick question: can I or can I not use bitwise-and with regular ints? For example, let's say that 9 was the input in index 3, could I still use bitwise-and between 9 and a mask? (1111) OR, do I need to convert the 9 into binary first & then use the bitmask? Thanks – SlackStack Sep 06 '17 at 02:00
  • i do not understand your meaning of indexes in this context. please define what it means. Any number of any denomination is just a set of bits. decimal '9' is the same as hex '9' is the same as octal '11' is the same as binarey '1001'. – Serge Sep 06 '17 at 02:16
  • I am putting integer values into a 32-bit integer x. I am taking a k-value from the user that tells me how many values they will be putting in (EX: k = 4). So then the user would input four values (EX: 4, 15, 8, 2). Each of those numbers would need to be taking up 8 bits for a total of 32 bits. I have to partition the bits into indices, for example 4 is in index 3, 15 is in index 2, and so on. What I am confused about is the following: if a user inputs 16 k-values, then the bits will have to divide into 2 bits per index (32/k), in which case I would have to have a mask with 30 0s and two 1s. – SlackStack Sep 06 '17 at 02:30
  • 1
    @Programme So, you will have to mask only 2 bits. your mask will look like this `0000...0011 (0х3)`. What are you confused about? you can have masks of any number of bits in the range of 1 to 32. Well, according to your description, if you use 2 bits per index, then the numbers which you can put into it are 0,1,2, and 3. There is no bit space for bigger numbers. if you use 4 bits, then you could accommodate 0..15, 6 bits will give you 0..63 and so on. – Serge Sep 06 '17 at 10:20
  • I ended up figuring this out in my own strange way after confusing myself for a couple hours. This really does help clarify completely, though. Thank you! I appreciate it. Sometimes it can be difficult to wrap your mind around things if you constantly overthink things. – SlackStack Sep 06 '17 at 10:25