How can I set the even bits to 1 using only specific operators?

  • Thread starter opt!kal
  • Start date
  • Tags
    Bits even
In summary, you are not allowed to use big constants such as 0xblackff. You are not allowed to use any control constructs such as if, do, while, for, switch, etc. You are expressly forbidden to use macros. You are not allowed to define any additional functions in this file. You are not allowed to call any functions. You are allowed to use only 8 legal operators. Some of the problems restrict the set of allowed operators even further. You may assume that your machine: Uses 2s complement, 32-bit representations of integers. Performs right shifts arithmetically. Has unpredictable behavior when shifting an integer by more than the word size.
  • #1
opt!kal
19
0

Homework Statement


Each "Expr" is an expression using ONLY the following:
1. Integer constants 0 through 255 (0xFF), inclusive. You are
not allowed to use big constants such as 0xblackff.
2. Function arguments and local variables (no global variables).
3. Unary integer operations ! ~
4. Binary integer operations & ^ | + << >>

Some of the problems restrict the set of allowed operators even further.
Each "Expr" may consist of multiple operators. You are not restricted to
one operator per line.

You are expressly forbidden to:
1. Use any control constructs such as if, do, while, for, switch, etc.
2. Define or use any macros.
3. Define any additional functions in this file.
4. Call any functions.
5. Use any other operations, such as &&, ||, -, or ?:
6. Use any form of casting.

You may assume that your machine:
1. Uses 2s complement, 32-bit representations of integers.
2. Performs right shifts arithmetically.
3. Has unpredictable behavior when shifting an integer by more
than the word size.

/*
* evenBits - return word with all even-numbered bits set to 1
* Legal ops: ! ~ & ^ | + << >>
* Max ops: 8
* Rating: 2
*/
int evenBits(void) {
return ?;
}

The Attempt at a Solution



I have no idea! What I originally thought was that I would have to perform a shift by 31 (whether left or right, I don't know) so that all the numbers would be set to all 0's or 1's and work from there, but then I realized that the odd bits should probably remain the same. I guess I could create an int var, of -1 or something and go from there, but really I just don't know if that would accomplish anything. Any help would be greatly appreciated.

P.S does anyone know of some links that could give me a better understanding of all these bitwise and logical operations? Because I also have to do stuff like: if x <= y then return 1, else return 0 / implement the ! operator, using all of the legal operators except !/ x+y+z using only a single '+', and I would love to get a better understanding why/how to do this, instead of fumbling around for hours on end. Thanks!
 
Physics news on Phys.org
  • #2
Both bytes - older and younger - will be identical. All you have to do then is to move one byte to the left and to combine it with the other one.
 
  • #3
Hrmm I think I see what you're saying, basically do a left shift by a byte (x << 4, correct?) and then or it with what the original was? But nothing is being passed into the function, and I need a variable in order to do the left shift correct? I guess I'm still having trouble comprehending how exactly the shift is done with nothing passed. Thanks for the help!
 
  • #4
opt!kal said:
x << 4, correct?

How many bits in byte?

But nothing is being passed into the function, and I need a variable in order to do the left shift correct? I guess I'm still having trouble comprehending how exactly the shift is done with nothing passed. Thanks for the help!

You don't need a variable, you can shift just a value. For example common way of combining characters into integers is

code = ('R' << 24) + ('I' << 16) + ('F' << 8) + 'F';

this is sometimes used to prepare constants that will be used in fast checking if the data starts with a given character sequence ("RIFF" in this case) - instead of doing string comparison you are doing much faster int comparison.

In case you wonder: RIFF stands for Resource Interchange File Format and all AVI and WAV files start with this sequence.
 
  • #5
*smacks self on forehead* 8, there are 8 bits in a byte!

So then I should probably have something like:

('A' << 8) | A correct?
 
  • #6
Seems OK. Just select correct value for A.
 

What does it mean to "set the even bits to 1"?

Setting the even bits to 1 refers to changing the value of the even-numbered bits in a binary number to 1 while leaving the odd-numbered bits unchanged. This can be done in various ways depending on the programming language or computer system being used.

Why would someone want to set the even bits to 1?

Setting the even bits to 1 can be useful in certain applications, such as error correction and data encryption. It can also be used to manipulate or extract specific information from a binary number.

How is this operation performed?

The method for setting the even bits to 1 may vary depending on the programming language or computer system being used. In general, it involves using bitwise operators or logical operations to manipulate the bits in a binary number.

Can the even bits be set to a value other than 1?

Yes, depending on the specific use case, the even bits can be set to any desired value. For example, in some encryption algorithms, the even bits may be set to a specific pattern of 0s and 1s for added security.

Are there any potential drawbacks to setting the even bits to 1?

In certain applications, setting the even bits to 1 may cause unintended consequences if not done correctly. It is important to understand the purpose and implications of this operation before using it in a program or system.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Precalculus Mathematics Homework Help
Replies
5
Views
754
  • Engineering and Comp Sci Homework Help
Replies
17
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
Back
Top