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

  • Thread starter Thread starter opt!kal
  • Start date Start date
  • Tags Tags
    Bits even
Click For Summary

Discussion Overview

The discussion revolves around a homework problem requiring participants to set all even-numbered bits of an integer to 1 using a limited set of operators. The focus is on understanding bitwise operations and how to manipulate bits without using control constructs or additional functions.

Discussion Character

  • Homework-related
  • Exploratory
  • Technical explanation

Main Points Raised

  • One participant expresses uncertainty about how to approach the problem, considering the need to shift bits and the implications of keeping odd bits unchanged.
  • Another participant suggests that combining two identical bytes through a left shift and an OR operation could be a solution.
  • A participant questions the necessity of a variable for performing a left shift, indicating confusion about how to manipulate values without function arguments.
  • Clarification is provided that shifting can be done directly on values, using examples of combining characters into integers.
  • One participant realizes the number of bits in a byte and proposes a specific operation involving a character shifted left.
  • Another participant confirms the proposed operation is acceptable, advising to select the correct value for the character.

Areas of Agreement / Disagreement

Participants generally agree on the approach of using bitwise operations, but there remains uncertainty about specific implementations and the necessity of variables for shifting.

Contextual Notes

Participants are working within strict constraints on the allowed operations and are exploring how to effectively use bitwise manipulation without additional constructs.

Who May Find This Useful

Readers interested in bitwise operations, programming challenges, or homework assistance in computer science may find this discussion beneficial.

opt!kal
Messages
18
Reaction score
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
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.
 
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!
 
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.
 
*smacks self on forehead* 8, there are 8 bits in a byte!

So then I should probably have something like:

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

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
17
Views
6K
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
4K